background script for changing state
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-01-2017 10:28 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-01-2017 10:35 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-01-2017 10:36 PM
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();
}
}
Harish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-01-2017 10:36 PM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-01-2017 10:40 PM
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