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

ifti122
Kilo 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. 

 

 

View solution in original post

9 REPLIES 9

Bhuvan
Kilo Patron

@snowsid88 

 

Refer below for sample, fine tune the encoded query as per your requirements

Bhuvan_0-1757341667509.png

Bhuvan_4-1757342906675.png

Bhuvan_5-1757342938297.png

Bhuvan_6-1757343074817.png

var inc_record=new GlideRecord('incident');
inc_record.addEncodedQuery("caller_id=a9b3967483842210ce6dfb96feaad339^priority=5");
inc_record.query();
while(inc_record.next())
{
inc_record.state='6';
inc_record.impact='3';
inc_record.urgency='2';
inc_record.assignment_group='287ebd7da9fe198100f92cc8d1d2154e';
inc_record.close_code='Solution provided';
inc_record.close_notes='Resolved by Background Script';
inc_record.update();
}

If this helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan

@snowsid88 

 

Did you get a chance to review this ?

 

As per community guidelines, you can accept more than one answer as accepted solution. If my response helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @snowsid88 

What is your use case here?
The priority is a combination of Impact and Urgency, and the property can be changed directly.

Also, why are you trying to set the Incident to Resolved while changing the priority from 5 to 4? That actually increases the priority, which doesn’t seem valid in this context.

It looks like this may not be a valid use case.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Ankur Bawiskar
Tera Patron
Tera Patron

@snowsid88 

Is this a valid business requirement or it's for learning purpose?

You are marking INC to Resolve on Degrading the Priority and updating Assignment Group.

But why?

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

Bhimashankar H
Mega Sage

Hey @snowsid88 ,

 

I hope you saw my reply. 

 

As per new community feature you can accept multiple responses as answers.
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Accept as Answer'. It will help future readers as well having similar kind of questions.

Thanks,
Bhimashankar H