Business Rules
Populate Fields on Insert
Automatically populate fields with default values or calculated values when a record is created.
(function executeRule(current, previous /*null when async*/) {
// Only run on new records
if (!current.isNewRecord()) {
return;
}
// Example 1: Set default priority based on caller's VIP status
if (current.caller_id.vip.toString() === 'true' && current.isNil('priority')) {
current.priority = '2'; // High priority for VIPs
current.work_notes = 'Priority set to High due to VIP caller';
}
// Example 2: Auto-populate due date based on priority
if (!current.isNil('priority') && current.isNil('due_date')) {
var priorityToDueDays = {
'1': 4, // Critical: 4 hours
'2': 8, // High: 8 hours
'3': 24, // Moderate: 24 hours
'4': 72, // Low: 72 hours
'5': 120 // Planning: 120 hours
};
var hoursToAdd = priorityToDueDays[current.priority.toString()] || 24;
var gdt = new GlideDateTime();
gdt.addHours(hoursToAdd);
current.due_date = gdt;
gs.info('Set due date to ' + gdt.getDisplayValue() + ' for priority ' + current.priority);
}
// Example 3: Set category based on configuration item
if (!current.cmdb_ci.nil() && current.isNil('category')) {
var ciClass = current.cmdb_ci.sys_class_name.toString();
var classToCategory = {
'cmdb_ci_computer': 'hardware',
'cmdb_ci_server': 'hardware',
'cmdb_ci_app_software': 'software',
'cmdb_ci_database': 'database',
'cmdb_ci_network_adapter': 'network'
};
if (classToCategory[ciClass]) {
current.category = classToCategory[ciClass];
current.work_notes = 'Category auto-populated based on CI type: ' + ciClass;
}
}
// Example 4: Copy location from caller if not set
if (!current.caller_id.nil() && current.isNil('location')) {
current.location = current.caller_id.location;
}
// Example 5: Generate unique reference number
if (current.isNil('u_reference_number')) {
var prefix = 'REF';
var timestamp = new GlideDateTime().getNumericValue();
current.u_reference_number = prefix + '-' + timestamp.toString().substring(8);
}
})(current, previous);How to use it
1. Create a before Business Rule on your table 2. Check "Insert" checkbox only 3. Customize the field population logic for your needs 4. Test by creating new records
Adapt the table names, fields, and conditions to your instance. Test the behavior in a development environment before using it in production.