need background script to close change and update comments
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â02-08-2021 10:04 PM
Hi Team,
I want to close 105 changes with additional comments" change closed manually". Please let me know the background script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â02-09-2021 03:56 AM
Did you get a chance to try my script? The place where you have kept state in addQuery() method, there you will need to keep number and statement should be like this:
chg.addQuery('number','IN','FG-CHG0031009,FG-CHG0031102,FG-CHG0031047...etc' );
But when you have numbers list present with you then you can achieve this even by not using any script also.
And I feel that manual way is faster as well.
Please follow the below steps:
1. Filter the change records with your number list by adding the filter condition as Number 'is one of' your list of numbers without any comma and 1 record in each line as shown below. Click run button
2. Right click hamburger icon on column header and select 'Update All'.
It will ask by showing you the list of records you want to update
Click 'OK'.
3. Then a form will be open in front of you. Add desired values there in one go and once you hit the update button all the records will be updated.
Please mark this as correct and helpful if it resolved your query.
Thanks,
Mohit Kaushik
Mohit Kaushik
ServiceNow MVP (2023-2025)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â02-08-2021 10:36 PM
You can use fix script for this
updateRecords();
function updateRecords(){
try{
var numberArray = 'CHG001,CHG002';
var changeRec = new GlideRecord('change_request');
changeRec.addQuery('number','IN', numberArray);
changeRec.query();
while(changeRec.next())
{
changeRec.state = 3; // closed
changeRec.active = false;
changeRec.comments = 'Change closed manually';
changeRec.setWorkflow(false);
changeRec.update();
}
}
catch(ex){
gs.info('Exception'+ex);
}
}
Regards
Ankur
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â02-08-2021 10:41 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â02-08-2021 10:43 PM
Hi Gaurav,
can you please provide me the code.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
â02-08-2021 10:48 PM
here it is,
please let me know whether it is working or not. also don't forget fill the required data of mandatory fields in while loop.
var chg=new GlideRecord("change_request");
//First Filter your query in change_request table and copy paste it over here
chg.addEncodedQuery("active=true^category=Hardware");
chg.query();
while(chg.next())
{
chg.state=3;
chg.comments="change close Automatically";
chg.setWorkflow(false);
chg.update();
gs.log("Performed the Change");//please do this activity for backtracking if any issue occurs
gs.print("Change closed Automatically");
}