Scripting

Ram012
Tera Contributor

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.

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Ram012 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Yaramala
Tera Contributor

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:

var gr = new GlideRecord('incident');
gr.addEncodedQuery("priorityIN1,2,3^assignment_group=^sys_created_onBETWEENjavascript:gs.dateGenerate('2024-01-01','00:00:00')@javascript:gs.dateGenerate('2024-12-31','23:59:59')^active=false");
gr.query();
while (gr.next()) {
    gs.info('Incident Number: ' + gr.number);
}
 

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!

 

Murtaza Saify
Tera Contributor

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.

javascript
Copy
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:

  1. Navigate to System Definition > Business Rules.

  2. 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:

      javascript
      Copy
      (function executeRule(current, previous /*null when async*/) {
          if (current.priority == 1) {
              current.short_description = 'High Priority';
          }
      })(current, previous);
  3. Save and Activate the Business Rule.

Viraj Hudlikar
Tera Sage

Hello @Ram012 

For Q1, you could do same over list view and use encoded query and enhance your script by using same.

VirajHudlikar_0-1740408964969.png

 

var grIncident = new GlideRecord('incident');
grIncident.addEncodedQuery("sys_created_onONLast year@javascript&colon;gs.beginningOfLastYear()@javascript&colon;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:

  1. Checks if the priority is set to 1.

  2. Ensures that "High Priority" is not already part of the short description.

  3. 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.