background script for changing state

Nikita35
Kilo Guru

hi all

can you advice how can I change the state from backend.

I want to change it from work in progress to complete.

Regards

Nikita Khavnekar

5 REPLIES 5

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Nikita,



Please refer sample script here and adjust your code accordingly. You can use addQuery in case you want to filter and update the records.


Try your script on dev first and then you can ship this on prod via fix script.






var updateGr = new GlideRecord('Pass TABLE NAME');


updateGr.query();


while(updateGr.next())


  {


  updateGr.state = 'Pass choice value here';


  updateGr.setWorkflow(false);


  updateGr.update();


  }







http://wiki.servicenow.com/index.php?title=GlideRecord#gsc.tab=0


Harish KM
Kilo Patron
Kilo Patron

try this in background


updateState();


function updateState(){


gr = new GlideRecord('change_request');


gr.addQuery('active','true');


gr.query();


while (gr.next()){


gr.state=3;//closed value


gr.setWorkflow(false); //Do not run business rules


gr.autoSysFields(false); //Do not update system field


gr.update();


}


}


Regards
Harish

nitish99
Tera Guru

Hi Nikita,



var _inc =new GlideRecord('incident');


_inc.addQuery('sys_id','sys_id_of_record');


_inc.query();


if(_inc.next()){


_inc.state='value of complete state';


_inc.update();


gs.info('Incident Number==>'+_inc.number);


}


amlanpal
Kilo Sage

Hi Nikita,



You have to write the background script like below:



var gr = new GlideRecord('change_request');


var string='active=true^state=10 ';   //Replace state value from '10' to 'In-progress' value. You can also add more queries.


gr.addEncodedQuery(string)


gr.query();


while(gr.next())


{


gr.setWorkflow(false);


gr.autoSysFields(false);


gr.state = 10;   //Replace state value to 'Completed' choice value from 10 as mentioned here.


gr.update();


}



I hope this helps.Please mark correct/helpful based on impact