Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

change state of incident

snowsid88
Tera Contributor

 

write a back ground script to set incident to resolve state while change priority to 5 to 4 and set assignment group to “network” in ServiceNow

1 ACCEPTED SOLUTION

M Iftikhar
Tera Sage

 

Hi @snowsid88,

 

You can achieve this using a background script in System Definition → Scripts – Background. The script below updates all incidents with priority 5, sets them to Resolved, changes priority to 4, and assigns them to the Network group:

 

(function() {
 
    var gr = new GlideRecord('incident');
    gr.addQuery('priority', 5);
    gr.query();

 
    var groupGR = new GlideRecord('sys_user_group');
    groupGR.addQuery('name', 'Network');
    groupGR.query();
    if (!groupGR.next()) {
        gs.print('Assignment group "Network" not found!');
        return;
    }
    var networkGroupSysId = groupGR.sys_id.toString();

 
    while (gr.next()) {
        gr.priority = 4;
        gr.assignment_group = networkGroupSysId;
        gr.state = 6; // Resolved
        gr.update();
        gs.print('Updated incident: ' + gr.number);
    }

    gs.print('Script execution completed.');
})();

 

 

Thanks & Regards, 
Muhammad Iftikhar 

If my response helped, please mark it as the accepted solution so others can benefit as well. 

 

 

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.

View solution in original post

9 REPLIES 9

M Iftikhar
Tera Sage

 

Hi @snowsid88,

 

You can achieve this using a background script in System Definition → Scripts – Background. The script below updates all incidents with priority 5, sets them to Resolved, changes priority to 4, and assigns them to the Network group:

 

(function() {
 
    var gr = new GlideRecord('incident');
    gr.addQuery('priority', 5);
    gr.query();

 
    var groupGR = new GlideRecord('sys_user_group');
    groupGR.addQuery('name', 'Network');
    groupGR.query();
    if (!groupGR.next()) {
        gs.print('Assignment group "Network" not found!');
        return;
    }
    var networkGroupSysId = groupGR.sys_id.toString();

 
    while (gr.next()) {
        gr.priority = 4;
        gr.assignment_group = networkGroupSysId;
        gr.state = 6; // Resolved
        gr.update();
        gs.print('Updated incident: ' + gr.number);
    }

    gs.print('Script execution completed.');
})();

 

 

Thanks & Regards, 
Muhammad Iftikhar 

If my response helped, please mark it as the accepted solution so others can benefit as well. 

 

 

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.

GlideFather
Tera Patron

Hi @snowsid88,

 

Priority is calculated as per values in Impact and Urgency, so in your code update the two fields instead of Priority as it is auto-calculated...

 

GlideFather_0-1757341688450.png

 

Remove:

gr.priority = 4;

 

and replace it with:

gr.urgency = 2;
gr.impact = 3;

//or

gr.urgency = 3;
gr.impact = 2;

 

 

Note: also, "gr" is also not recommended variable name but it's not for scope of this question ;))

_____
This reply is 100 % GlideFather and 0 % AI

@snowsid88 

and also when you Resolve an incident, Close code and notes are mandatory:

GlideFather_0-1757342501279.png

 

so add:

gr.close_notes = 'Closed by a script';
gr.close_code = 'Solution provided';

 

_____
This reply is 100 % GlideFather and 0 % AI

Bhimashankar H
Mega Sage

Hi @snowsid88 ,

 

Use below working code and ensure the you have added the correct filter on incident table. In script it is processing Active incident record with priority=5.

 

// Bulk update active Incidents: priority 5 -> 4, set Resolved, assign to "Network"
(function() {
  // Look up the "Network" group sys_id once
  var networkGroupId = '';
  var grp = new GlideRecord('sys_user_group');
  grp.addQuery('name', 'Network');         // adjust name if different or you can directly give the sys_id of your network group to above networkGroupId variable
  grp.addActiveQuery();
  grp.setLimit(1);
  grp.query();
  if (grp.next()) {
    networkGroupId = grp.getUniqueValue();
  } else {
    gs.error('Assignment group "Network" not found or inactive. Aborting.');
    return;
  }


  var inc = new GlideRecord('incident');
   inc.addActiveQuery();                    // only active
   inc.addQuery('priority', '5');           // current priority is 5
  // Set desired values before updateMultiple()
  //inc.setValue('priority', '4'); //Directly it won't work
  inc.setValue('impact','2');
  inc.setValue('urgency','3');          // change priority 5 -> 4
  inc.setValue('incident_state', '6');     // 6 is  Resolved
  inc.setValue('state', '6');              // keep in sync, helpful in some UIs
  inc.setValue('assignment_group', networkGroupId);

  // Optional: populate required resolution fields to satisfy business rules
  inc.setValue('close_code', 'Solved (Work Around)');
  inc.setValue('close_notes', 'Bulk auto-resolve on priority change 5→4.');

  // Optional safety: prevent workflows/notifications for mass change
  // inc.setWorkflow(false);
  // inc.autoSysFields(false); // if not wanting sys_updated_* to change

  var updated = inc.updateMultiple();      // applies to all records matching the query
  gs.info('Incidents updated (priority 5→4, resolved, assigned to Network): ' + updated);
})();

 

Thanks,
Bhimashankar H

 

-------------------------------------------------------------------------------------------------
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. Thanks!