I understand that the previous code may look similar, but it’s crucial to ensure you're using precise steps and checks for Sys IDs, especially for reference fields. Let’s break this down further to ensure all steps are covered correctly, and I'll provide more detailed error handling for debugging.
Complete Step-by-Step Solution
-
Navigate to Business Rules:
- Log into your ServiceNow instance.
- Go to
System Definition > Business Rules.
-
Create a New Business Rule:
- Click
New to create a new Business Rule.
-
Basic Information:
- Name: Provide a meaningful name, e.g.,
Set BU Head.
- Table: Select
alm_hardware.
- Active: Ensure this is checked.
- When: Select
before.
-
Conditions:
- Optionally, add conditions to limit when this rule runs. For example, you can configure it to run only when
assigned_to is updated.
-
Script:
- Go to the
Advanced tab.
- Enter the following script:
(function executeRule(current, previous /*null when async*/) {
try {
// Only proceed if 'assigned_to' is populated and has changed.
if (current.assigned_to && current.assigned_to.changes()) {
// Query the sys_user table for the assigned user's record.
var userGR = new GlideRecord('sys_user');
if (userGR.get(current.assigned_to)) {
// Get the department Sys ID from the user record.
var deptID = userGR.department;
if (deptID) {
// Query the cmn_department table for the department record.
var deptGR = new GlideRecord('cmn_department');
if (deptGR.get(deptID)) {
// Get the department head Sys ID.
var deptHeadID = deptGR.department_head;
if (deptHeadID) {
// Set the u_bu_head field with the department head Sys ID.
current.u_bu_head = deptHeadID;
} else {
gs.log("Department head not found for department: " + deptID);
}
} else {
gs.log("Department record not found for department ID: " + deptID);
}
} else {
gs.log("Department field not set for user: " + current.assigned_to);
}
} else {
gs.log("User record not found for sys_id: " + current.assigned_to);
}
}
} catch (e) {
gs.error("Error in Set BU Head Business Rule: " + e.message);
}
})(current, previous);
Explanation:
-
Basic Validations:
- The script starts with a validation to check if the
assigned_to field is set and if it has changed (current.assigned_to.changes()).
-
Query sys_user table:
- It queries the
sys_user table using the Sys ID in current.assigned_to.
-
Retrieve Department Sys ID:
- From the user record, it retrieves the
department Sys ID.
-
Query cmn_department table:
- Using the department Sys ID, it queries the
cmn_department table to get the department record.
-
Retrieve and Set Department Head Sys ID:
- From the department record, it retrieves the
department_head Sys ID and sets it to current.u_bu_head.
-
Error Handling and Logging:
- The script includes error handling with meaningful log entries for each potential failure point. This will help you identify any issues during execution.
-
Save and Test:
- Save the Business Rule.
- Test it by updating an asset's
assigned_to field in the alm_hardware table.
- Check the logs (System Logs > All) for any debug messages.
Debugging Tips:
- Check Logs: Verify logs to see where the process might be failing based on the log messages.
- Valid Data: Ensure that the
assigned_to field is being set with valid user Sys IDs and those users have valid department references.
- Ensure Permissions: Make sure the script user has adequate permissions to read the necessary fields in both
sys_user and cmn_department.
If you've followed these steps and the rule still doesn't work as expected, please review the logs for any errors and ensure the appropriate data integrity in your tables.