To achieve the requirement of allowing the Assignment Group to be changed only three times in an incident and then preventing further changes, you can use a combination of client-side scripting (Client Script) and server-side scripting (Business Rule) in ServiceNow.
Here’s a step-by-step guide:
Step 1: Create a Field for Tracking Changes
First, create a custom field to track the number of times the Assignment Group has been changed. This field will be an integer field on the Incident table.
- Go to Studio > Create Application File > Table.
- Select the Incident table (or go to Incident → Create new).
- Click "Add New Field".
- Name the field something like
u_assignment_group_change_count.
- Set the type to Integer.
- Save the field.
Step 2: Create a Business Rule to Track Changes
Next, create a Business Rule to increment the change count every time the Assignment Group is changed.
- Navigate to System Definition > Business Rules.
- Click "New" to create a new Business Rule.
- Configure the Business Rule:
- Name: Increment Assignment Group Change Count
- Table: Incident [incident]
- When: Before
- Update: true
- Filter Conditions: [Assignment Group] [changes].
- Under the Script tab, add the following script:
(function executeRule(current, previous /*null when async*/) {
if (current.assignment_group != previous.assignment_group) {
current.u_assignment_group_change_count += 1;
}
})(current, previous);
- Save the Business Rule.
Step 3: Create a Client Script to Prevent Further Changes
Create a Client Script to prevent changes to the Assignment Group once the count reaches three.
- Navigate to System Definition > Client Scripts.
- Click "New" to create a new Client Script.
- Configure the Client Script:
- Name: Prevent Assignment Group Changes After Three Changes
- Table: Incident [incident]
- Type: onChange
- Field: assignment_group
- Inherited: False
- Under the Script tab, add the following script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Make a GlideAjax call to get the change count
var ga = new GlideAjax('ChangeGroupHelper');
ga.addParam('sys_id', g_form.getUniqueValue());
ga.getXMLAnswer(function(answer) {
var count = parseInt(answer, 10);
if (count >= 3) {
g_form.showErrorBox('assignment_group', 'Assignment Group cannot be changed more than three times.');
g_form.setValue('assignment_group', oldValue);
}
});
}
Step 4: Create a GlideAjax Script Include
For the client script to interact with the server-side logic, create a Script Include.
- Navigate to System Definition > Script Includes.
- Click "New" to create a new Script Include.
- Configure the Script Include:
- Name: ChangeGroupHelper
- Client Callable: True
- Script:
var ChangeGroupHelper = Class.create();
ChangeGroupHelper.prototype = Object.create(GlideAjax.prototype);
ChangeGroupHelper.prototype.getChangeCount = function() {
var incidentSysId = this.getParameter('sys_id');
var incidentGR = new GlideRecord('incident');
incidentGR.get(incidentSysId);
return incidentGR.u_assignment_group_change_count.toString();
};
- Save the Script Include.
This configuration should enable you to track the number of times the Assignment Group field is changed and prevent further changes after it has been modified three times. The changes take into account both client-side and server-side scripting to ensure enforcement integrity.