In servicenow please fix this after update business rule, and tell me what is the correct type of field in custom table i store display value in string field or can i use reference only. Please check and fix the code. if (current.assignment_group.changes()) {
var changeRecord = new GlideRecord('u_custom_assignment_change'); // Assuming u_custom_assignment_change is your custom table
changeRecord.initialize();
changeRecord.u_incident_number = current.number;
changeRecord.u_old_assignment_group = current.assignment_group.previous.display_value;
changeRecord.u_new_assignment_group = current.assignment_group.display_value;
changeRecord.u_changed_by = gs.getUserID();
changeRecord.u_change_timestamp = new GlideDateTime();
changeRecord.insert();
}
Assistant
In ServiceNow, it looks like you're trying to track changes to the assignment_group field in incidents by recording them in a custom table. Let's address your questions and fix your code:
Choosing the Correct Field Type
Reference Field: It's generally better to store a reference to another table if you want to maintain a live link to the record. This will store the sys_id of the referenced record but will display the display value naturally. This is ideal for fields like assignment_group, which refer to another record in the sys_user_group table.
String Field: Use this if you only need the display value at the time of the change and it's fine if it doesn't update automatically if the reference record changes.
In your scenario, if you want to maintain historical records of both the old and new group names as they were at the time of the change, storing them as strings is appropriate. However, if you are okay with just tracking sys_ids, use reference fields.
Correcting the Code
Here's the corrected version of your business rule's script logic:
javascript
if (current.assignment_group.changes()) {
// Assuming 'u_custom_assignment_change' is your custom table
var changeRecord = new GlideRecord('u_custom_assignment_change');
changeRecord.initialize();
// Store the incident number
changeRecord.u_incident_number = current.number;
// These lines have been updated to correctly access the display values
changeRecord.u_old_assignment_group = current.assignment_group.previous.getDisplayValue();
changeRecord.u_new_assignment_group = current.assignment_group.getDisplayValue();
// Tracking who changed the assignment group
changeRecord.u_changed_by = gs.getUserID();
// Recording the timestamp of the change
changeRecord.u_change_timestamp = new GlideDateTime();
// Insert the new record into the custom table
changeRecord.insert();
}
Explanation of the Code
current.assignment_group.changes(): This checks if there is any change to the assignment_group field.
getDisplayValue(): This method is used to get the display value of a reference field. current.assignment_group.previous.getDisplayValue() gets the old display value, and current.assignment_group.getDisplayValue() gets the new display value.
gs.getUserID(): This returns the sys_id of the user who made the change.
GlideDateTime: This object gets the current date and time, which is useful for tracking when changes occur.
Make sure that the fields (u_incident_number, u_old_assignment_group, u_new_assignment_group, u_changed_by, u_change_timestamp) exist in your u_custom_assignment_change table and are of the appropriate type (String for text values, Reference for storing full reference links, etc.).