Fix script - change state

Navneet3
Tera Expert

Hi

I have a change request which is stuck in Assess state. The change has been approved and implemented, I need to implement the change.

Here is the background script I ran in DEV instance, the script changed all the changes to implement.

what is wrong with the script?

 

var gr= new GlideRecord('change_request');
gr.addEncodedQuery("CHG0000186");

gr.query();

while(gr.next()){

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

}

 

thank you

1 ACCEPTED SOLUTION

Brian Lancaster
Tera Sage

I see a couple things here. Your Encoded Query just was a change number but should say some more along the lines of "number=CHG0000186". Because of this it is ignoring your Encoded Query and querying the entire table. Since you are also only updating one change I would do a if (gr.next()) instead of a while. Since you query is very simple I would not use encoded query. That is for when you have something vary complicated with may a bunch of or statement or date ranges. Try this as your code.

 

var gr= new GlideRecord('change_request');
gr.addQuery("number", "CHG0000186");
gr.query();
if(gr.next()){
     gr.setWorkflow(false); //prevent running business rule
     gr.state='-1';//implement state
     gr.update();
}

View solution in original post

3 REPLIES 3

Mike_R
Kilo Patron
Kilo Patron

The issue is with your encoded query. Try this

 

var chg = new GlideRecord('change_request');
chg.get('sys_id_of_record_to_update');
chg.setWorkflow(false); //prevent running business rule
chg.state = -1;
chg.update();

 

 

Brian Lancaster
Tera Sage

I see a couple things here. Your Encoded Query just was a change number but should say some more along the lines of "number=CHG0000186". Because of this it is ignoring your Encoded Query and querying the entire table. Since you are also only updating one change I would do a if (gr.next()) instead of a while. Since you query is very simple I would not use encoded query. That is for when you have something vary complicated with may a bunch of or statement or date ranges. Try this as your code.

 

var gr= new GlideRecord('change_request');
gr.addQuery("number", "CHG0000186");
gr.query();
if(gr.next()){
     gr.setWorkflow(false); //prevent running business rule
     gr.state='-1';//implement state
     gr.update();
}

Thank you, that worked