in servicenow incident table, i want that my assignment group can change only 3 times, if the count is greater than 3 then abort the action
Assistant
User
not owrking
Assistant
User
syshistory line table is not correct
Assistant
To enforce a rule in ServiceNow that restricts the number of times the assignment group can be changed for an incident record to a maximum of 3 times, you can use a business rule. This business rule will be triggered before the record is updated and will check the assignment group change count. If the count exceeds 3, it will abort the action and show an error message.
Here's how you can implement this:
Steps to Create the Business Rule
Navigate to Business Rules:
Go to your ServiceNow instance.
In the application navigator, type Business Rules and click on it.
Create a New Business Rule:
Click on the top right "New" button.
Basic Configuration:
Name: Give your business rule a descriptive name (e.g., Limit Assignment Group Changes).
Table: Select Incident [incident] from the table dropdown.
Advanced: Check the "Advanced" checkbox.
When to Run:
When: Select Before.
Insert: Uncheck this.
Update: Check this.
Delete: Uncheck this.
Condition:
You can either leave this blank or add conditions if required.
Script:
In the script section, use the following code to track the changes in the assignment group:
javascript
(function executeRule(current, previous /*null when async*/) {
// Check if the assignment_group field has changed
if (current.assignment_group.changes()) {
// Initialize a counter for assignment group changes
var count = 0;
// Query the incident record's history to count group changes
var grHistory = new GlideRecord('sys_history_line');
grHistory.addQuery('documentkey', current.sys_id);
grHistory.addQuery('field', 'assignment_group');
grHistory.query();
// Count the number of history records for assignment_group changes
count = grHistory.getRowCount();
// If the count exceeds 3, abort the action
if (count >= 3) {
gs.addErrorMessage('The assignment group can only be changed 3 times. You have reached the limit.');
current.setAbortAction(true); // Abort the update
}
}
})(current, previous);
Explanation of the Script:
The script checks if the assignment_group field has changed using current.assignment_group.changes().
If it has changed, it queries the sys_history_line table to find out how many times this field has been modified for the current incident record.
It counts the number of changes. If the count is 3 or more, it shows an error message and aborts the update action.
Save:
Click on "Submit" or "Update" to save the business rule.
Testing
Create an incident or update an existing one, then change the assignment group up to 3 times to confirm that it works as expected. On the 4th change, it should show the error message and not allow the change.
Note
This script may need to be adjusted based on your specific use case or customization within your ServiceNow instance.
Ensure you have the right permissions to create and modify business rules in your ServiceNow instance.
I apologize for any inconvenience. Let's go through the process step-by-step with additional details to troubleshoot and ensure the business rule works as intended.
Revised Steps to Create the Business Rule
Navigate to Business Rules:
Open your ServiceNow instance.
In the left-hand application navigator, type Business Rules and click to open it.
Create a New Business Rule:
Click the New button to create a new business rule.
Basic Configuration:
Name: Enter a descriptive name (e.g., Limit Assignment Group Changes).
Table: Choose Incident [incident].
Advanced: Check the "Advanced" checkbox.
When to Run:
When: Select Before.
Insert: Uncheck this.
Update: Check this.
Delete: Uncheck this.
Condition:
Leave it blank to apply the rule for all updates, or add conditions if necessary (e.g., current.active == true).
Script: Replace the contents of the script section with this refined script:
javascript
(function executeRule(current, previous /*null when async*/) {
// Only check if the assignment_group field has changed
if (current.assignment_group.changes()) {
// Initialize a counter for assignment_group changes
var count = 0;
// Query incident's sys_history_line to find assignment_group changes
var grHistory = new GlideRecord('sys_history_line');
grHistory.addQuery('documentkey', current.sys_id);
grHistory.addQuery('field', 'assignment_group');
grHistory.query();
// Count the number of changes in history
while (grHistory.next()) {
count++;
}
// Check if it exceeds 3 changes
if (count >= 3) {
gs.addErrorMessage('The assignment group can only be changed 3 times. You have reached the limit.');
current.setAbortAction(true); // Abort the update
}
}
})(current, previous);
Key Considerations
Permissions: Ensure that the user making the changes has the permissions to view and update the incident record.
Field Name: Verify that the field name in your instance is indeed assignment_group and that it’s spelled correctly in the script.
Testing Environment: Make sure you are testing in an appropriate environment. Business rules can behave differently depending on the instance configuration.
Logs: Utilize the JavaScript log to identify issues. You can use gs.info() to log messages at various points in the script.
History Visibility: Check if there are any existing records in the sys_history_line for testing. If none exist, ensure that there are changes made to the assignment group field before running the test.
Troubleshooting Tips
Add Debugging Information:
To help diagnose the issue, you can add debugging messages to the script as follows:
javascript
gs.info('Assignment group changed detected.');
// After counting the changes
gs.info('Count of assignment group changes: ' + count);
Check System Logs:
After testing the incident update in ServiceNow, look at the system logs (System Logs > All) for the debugging messages you added.
Review Business Rule Activation:
Make sure that the business rule is active. Check the box for "Active" in the business rule form.
Script Background:
Ensure that there are no other business rules or scripts that might interfere with your logic when changing the assignment group.
Testing without Changes:
Initially, perform a test by updating the assignment group once, then twice, and finally three times to confirm that the rule is triggered correctly.
By verifying these points and the steps outlined, you should be able to get this business rule functioning as intended. If issues persist, please share any error messages or unexpected behaviors you encounter, and I can provide further assistance.
I apologize for the oversight regarding the sys_history_line usage. In ServiceNow, to track changes to a field properly, it’s typically done through a reference table known as sys_update_set or by maintaining a custom field or counter. Unfortunately, tracking certain types of field changes via the history lines does not directly report counts in the way we might expect.
Instead, you may need to create a custom approach to count the changes made to the assignment_group field. Here’s an alternative solution.
Updated Approach: Using a Custom Field to Track Changes
Create a Custom Field:
You'll need to create a custom integer field on the Incident table to keep track of how many times the assignment group has changed.
Navigate to the Incident table (System Definition > Tables > Incident).
Add a new integer field (e.g., u_assignment_group_change_count).
Set up Business Rule:
Create a new business rule as described earlier, but with modifications for maintaining the count.
Steps to Create the Business Rule
Navigate to Business Rules:
Open your ServiceNow instance.
In the left-hand application navigator, type Business Rules and click to open it.
Create a New Business Rule:
Click New to create a new business rule.
Basic Configuration:
Name: Limit Assignment Group Changes.
Table: Incident [incident].
Advanced: Check the box for "Advanced".
When to Run:
When: Select Before.
Insert: Uncheck.
Update: Check.
Delete: Uncheck.
Script: Add the following script to count the changes:
javascript
(function executeRule(current, previous /*null when async*/) {
// Check if the assignment_group field has changed
if (current.assignment_group.changes()) {
// Initialize the custom counter field
var changeCount = current.u_assignment_group_change_count || 0;
// Increment the count
changeCount++;
// Check if the count exceeds 3
if (changeCount > 3) {
gs.addErrorMessage('The assignment group can only be changed 3 times. You have reached the limit.');
current.setAbortAction(true); // Abort the update
} else {
// If within limit, assign the new count back to the record
current.u_assignment_group_change_count = changeCount;
}
}
})(current, previous);
Explanation of the Script
The script checks if the assignment_group field has changed.
It retrieves the current value of the custom counter field u_assignment_group_change_count. If it’s null (e.g., on the first change), it initializes it to 0.
It increments the counter and checks if it exceeds 3.
If it does exceed 3, an error message is displayed and the action is aborted.
If it’s within limits, the new count is saved back to the record.
Testing
Perform tests to ensure that the assignment group change count is being tracked correctly:
Change the assignment group once, twice, and three times to check that the changes are counted.
On the 4th change, you should see the error message stating that the limit has been reached.
Cleaning Up
If this solution works as expected, you might want to reset the u_assignment_group_change_count back to 0 when you change the state of the incident to a resolved or closed state. You can do this with another business rule or through your incident workflow.
Conclusion
This method ensures that you have a reliable count of how many times the assignment group has been changed while keeping the code straightforward and leveraging a custom field to store the counter. If you need any more help or adjustments, feel free to ask!