need background script to close change and update comments

great
Kilo Contributor

Hi Team,

I want to close 105 changes with additional comments" change closed manually". Please let me know the background script 

12 REPLIES 12

@great ,

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

find_real_file.png

2. Right click hamburger icon on column header and select 'Update All'.

find_real_file.png

It will ask by showing you the list of records you want to update

find_real_file.png

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.

find_real_file.png

 

Please mark this as correct and helpful if it resolved your query.

 

Thanks,

Mohit Kaushik

 

 

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

Ankur Bawiskar
Tera Patron
Tera Patron

@great 

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

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

Gaurav Shirsat
Mega Sage

Hi

Please Find below Screen Shot

find_real_file.png

Please Mark Correct and Helpful

Thanks and Regards

Gaurav Shirsat

Hi Gaurav,

can you please provide me the code.

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");
}