How to write a fix script to delete multiple records from a table

test1231998
Tera Contributor

Hi I need to run a fix script to delete multiple records from a custom table if the class is Windows server and created by is me.

Initially to check it I wrote the below fix script(to see the exact count of the records in the logs-couldnt see that as well), which isnt working, Can anyone help me on it.

var gr = new GlideRecord('u_ci_loader_for_servers');
gr.addQuery("u_class="+cmdb_ci_win_server);
gr.query();
while(gr.next()){

// gr.deleteMultiple();
// gr.update();
gs.info(gr.getRowCount() + "Test");
}

9 REPLIES 9

Hi,

Indeed it does 🙂

Glad it helped you.

If the author returns here, seeing as this is from over a year ago, if you could mark my reply above as Correct, so that others can find it faster and easier, that'd be great.

Take care!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Prasad Pagar
Mega Sage

Hi Try below one

var gr = new GlideRecord('u_ci_loader_for_servers');
gr.addEncodedQuery("opened_byDYNAMIC90d1921e5f510100a9ad2572f2b477fe^u_class="+cmdb_ci_win_server);
gr.query();
gr.deleteMultiple();

Please mark my answer correct/helpful if applicable.

Thank you
Prasad

Ankur Bawiskar
Tera Patron
Tera Patron

@deepika 

Sample script is already shared by Anirudh

Just adding to his point.

1) wrap the code inside function

2) give correct user id here

deleteRecords();

function deleteRecords(){

var gr = new GlideRecord('u_ci_loader_for_servers');
gr.addEncodedQuery("u_class="+cmdb_ci_win_server);
gr.addQuery("sys_created_by" , 'yourUserIdHere'); // give the user id here
gr.query();
gr.deleteMultiple();

}

Regards
Ankur

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

@deepika 

Hope you are doing good.

Would you mind marking the best matching answer as correct and helpful, to close this thread?

Regards
Ankur

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

@deepika 

Hope you are doing good.

Would you mind marking the best matching answer as correct and helpful, to close this thread?

Regards
Ankur

 

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