How to Print a Worklog on a Inident after an Assignment Rule take an action ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 04:38 PM
Hello everyone,
I hope this is not a duplicated question, my question is, i have a lot of Assignment rules, we would like to know which ones are being triggered, my idea would be to have an message to be print on the ticket after a rules is used so we can identify which one the Assignment Rule used.
Example. I have rule A, B, C.
When my incident is created i want a message like the one below on the work log to identify it.
"This incident used the Rule C for assignment"
I was thinking in doing this using the scripts but the question is, how to do it ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 07:48 PM - edited 07-03-2024 07:49 PM
Hi @CainanO ,
To achieve logging or recording which assignment rule was triggered when an incident is created in ServiceNow, you can utilize Business Rules. Business Rules in ServiceNow allow you to run server-side scripts based on conditions and events, such as when a record is inserted (created), updated, or deleted.
Here’s how you can implement this:
1. Create a Business Rule
- Navigate to System definition -> Business Rule
- Click on New to create a new Business Rule.
- Provide a Name (e.g., Log Assignment Rule).
- Set the Table to Incident (or the table where your incidents are created).
- Configure the When to Run conditions:
- When: Before
- Insert: Checked (this means the script runs when a new record is inserted)
2. Write the Script to Log Assignment Rule
In the Business Rule, write a script that determines which assignment rule was used and logs this information to the Work Notes or Work Log of the incident. Here’s an example script assuming you want to log the assignment rule used:
(function executeRule(current, previous /* GlideRecord */) {
// Check if assignment rule was applied
if (current.assignment_group.changes() || current.assignment_group.nil()) {
// Get the name of the assignment rule that ran
var assignmentRule = current.assignment_source.getDisplayValue();
// Construct the log message
var logMessage = "This incident used the Rule " + assignmentRule + " for assignment";
// Add a work note with the log message
current.work_notes = logMessage + "\n" + current.work_notes;
}
})(current, previous);
Script Explanation:
- current: Represents the incident record currently being inserted.
- previous: Represents the incident record before the insert (in case of updates).
- current.assignment_group.changes(): Checks if the assignment group field changed during the assignment process.
- current.assignment_group.nil(): Checks if the assignment group field is empty (which could indicate no assignment rule was applied directly to assignment group).
- current.assignment_source.getDisplayValue(): Retrieves the display value of the assignment source (assignment rule).
3. Save and Test
- Save the Business Rule.
- Create a new incident to test. When the incident is created, the Business Rule script will run.
- Check the Work Notes or Work Log of the incident to see if the log message was added.
By using a Business Rule like this, you can effectively log which assignment rule was applied to each incident when it is created in ServiceNow. This approach provides visibility and auditability regarding how incidents are assigned based on your assignment rules.
If my reply helped with your issue please mark helpful 👍 and correct ✔️ if your issue is resolved.
By doing so you help other community members find resolved questions which may relate to an issue they're having
Thanks,
Astik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2024 02:17 PM
Astik, thanks for your update, i have tested here however whenever i submit a new incident i receive the following message.
What do you believe that could be causing this ?
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2024 02:43 PM
Also let me share a few information about how we submit incidents as it could be relevant, most of our system, service bridge and other automation generates the Incidents with the Assignment in Blank/Empty i dont know if this is relevant.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2024 02:43 AM
Hi @CainanO,
I don't believe current.assignment_source.getDisplayValue() is a valid value/function.
I was hoping there would be an OOB way of debugging the Assignment Rule, but I wasn't able to find one.
I did find a workaround by using the Assignment Rule's script, for example:
However, I noticed that this only works when you are setting the assignment group/assigned to via the script.
This does not work when you are setting the field(s) in the Assign to tab. (I am guessing the script doesn't get executed if either of the fields is set by the Assign to tab).
But what is the business requirement here? If it's simply for a debugging purpose, I don't think adding a work note would be ideal for the actual users.
Cheers