Scripting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2025 08:50 PM
Write a GlideRecord query to retrieve all inactive incidents created in the year 2024 with priority 1, 2, or 3 and an empty assignment group?
Write a Business Rule script that automatically sets the "Short Description" field to "High Priority" when the priority is set to 1?
Any idea please let me know.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2025 08:54 PM
what did you start with and what didn't work?
Unless you start you won't learn
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2025 09:21 PM
Hello @Ram012
Write a Glide Record query to retrieve all inactive incidents created in the year 2024 with priority 1, 2, or 3 and an empty assignment group?
Solution:
Write a Business Rule script that automatically sets the "Short Description" field to "High Priority" when the priority is set to 1?
Condition : prioroty is 1.
update : true
Insert : true
(function executeRule(current, previous /*null when async*/) {
current.short_description = 'High Priority';
}
})(current, previous);
If this addresses your question, please mark this response as Accepted Solution and/or give it a Kudos if you find it helpful.
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2025 05:58 AM
1. GlideRecord Query to Retrieve Inactive Incidents
This query retrieves all inactive incidents created in the year 2024 with priority 1, 2, or 3 and an empty assignment group.
var incidentGR = new GlideRecord('incident'); incidentGR.addQuery('active', false); // Inactive incidents incidentGR.addQuery('priority', 'IN', ['1', '2', '3']); // Priority 1, 2, or 3 incidentGR.addQuery('assignment_group', ''); // Empty assignment group incidentGR.addQuery('sys_created_on', '>=', '2024-01-01 00:00:00'); // Created on or after Jan 1, 2024 incidentGR.addQuery('sys_created_on', '<=', '2024-12-31 23:59:59'); // Created on or before Dec 31, 2024 incidentGR.query(); while (incidentGR.next()) { gs.print('Incident Number: ' + incidentGR.number + ', Priority: ' + incidentGR.priority); }
2. Business Rule Script to Set "Short Description" to "High Priority"
This Business Rule script automatically sets the short_description field to "High Priority" when the priority is set to 1.
Steps:
Navigate to System Definition > Business Rules.
Create a new Business Rule:
Name: Set Short Description for Priority 1
Table: Incident
Advanced: Checked
When to Run: Before (to ensure the change is saved)
Conditions:
priority changes to 1
Script:
javascriptCopy(function executeRule(current, previous /*null when async*/) { if (current.priority == 1) { current.short_description = 'High Priority'; } })(current, previous);
Save and Activate the Business Rule.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2025 07:05 AM
Hello @Ram012
For Q1, you could do same over list view and use encoded query and enhance your script by using same.
var grIncident = new GlideRecord('incident');
grIncident.addEncodedQuery("sys_created_onONLast year@javascript:gs.beginningOfLastYear()@javascript:gs.endOfLastYear()^active=false^priorityIN1,2,3^assignment_groupISNOTEMPTY");// This I got from list view.
grIncident.query();
while (grIncident.next()) {
gs.print('Incident Number: ' + gr.number);
}
For Q2, A Before Insert/Update BR with Name = "Prepend High Priority to Short Description"
// Check if the priority is set to 1 and "High Priority" is not already in the short description
if (current.priority == 1 && !current.short_description.includes("High Priority")) {
// Prepend "High Priority" to the existing short description
current.short_description = "High Priority - " + current.short_description;
}
This script:
Checks if the priority is set to 1.
Ensures that "High Priority" is not already part of the short description.
Prepends "High Priority" to the existing short description.
If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.
Thanks & Regards
Viraj Hudlikar.