Background Script - Change State

ToniOxley
Kilo Expert

Good Morning,

 

Please can someone advise... i need a background script to force two CHG records to the state Implement. We have discovered a fault within the change workflow and whilst we get that sorted.. we need to manually force a couple of changes to "implement" so we can proceed with them.

Advise/the script we need to run... would be much appreciated.

find_real_file.png

As you can see.. i am unable to change the state from the list view (or the form view)

find_real_file.png

thanks again!

1 ACCEPTED SOLUTION

Suseela Peddise
Kilo Sage

Hi,

As per OOB normal change state model , change can we move from authorize to schedule/cancel states only. .

See 'ChangeRequestStateModel_normal' script include.

So, you need use setWorkflow to false before running the script in backgroud or else business rule will stop doing that. Try below script:

var gr= new GlideRecord('change_request');
gr.addEncodedQuery("numberINCHG0030712,CHG0030714");

gr.query();

while(gr.next()){

gr.setWorkflow(false); //prevent running business rule
gr.state='-1';//implement state
gr.update();

}

 

Hope this helps!

If I have answered your question, please mark my response as correct and/or helpful.

Thanks,

Suseela P.

View solution in original post

5 REPLIES 5

Dhananjay Pawar
Kilo Sage

var changeGR=new GlideRecord('change_request');

changeGR.addEncodedQuery('number=CHG0040007^ORnumber=CHG0040006'); // replace with correct change number

 

changeGR.query();

while(changeGR.next()){

changeGR.state='3'; // replace state value here

changeGR.update();

}

 

Thanks,

Dhananjay.

Just add changeGR.setWorkflow(false); in above script before setting value to state

Ankur Bawiskar
Tera Patron
Tera Patron

@ToniOxley 

try this script in one time scheduled job

Ensure you give valid change numbers, valid choice values when you execute

updateRecords();

function updateRecords(){

try{

var numbers = ['CHG001', 'CHG002']; // give the change numbers here

var rec = new GlideRecord('change_request');

rec.addQuery('number', 'IN', numbers);

rec.query();

while(rec.next()){

rec.state = -1; // give valid choice value here

rec.setWorkflow(false); // to avoid triggering any BR

rec.update();

}

}

catch(ex){

gs.info('Exception is:' + ex)

}

}

Regards
Ankur

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

Suseela Peddise
Kilo Sage

Hi,

As per OOB normal change state model , change can we move from authorize to schedule/cancel states only. .

See 'ChangeRequestStateModel_normal' script include.

So, you need use setWorkflow to false before running the script in backgroud or else business rule will stop doing that. Try below script:

var gr= new GlideRecord('change_request');
gr.addEncodedQuery("numberINCHG0030712,CHG0030714");

gr.query();

while(gr.next()){

gr.setWorkflow(false); //prevent running business rule
gr.state='-1';//implement state
gr.update();

}

 

Hope this helps!

If I have answered your question, please mark my response as correct and/or helpful.

Thanks,

Suseela P.