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

AnirudhKumar
Mega Sage
Mega Sage
var gr = new GlideRecord('u_ci_loader_for_servers');
gr.addEncodedQuery("u_class="+cmdb_ci_win_server);
gr.addQuery("sys_created_by" , gs.getUserName());
gr.query();

gs.info(gr.getRowCount());

while(gr.next()){

gr.deleteRecord();

}

Allen Andreas
Administrator
Administrator

Hi,

You can use something like:

var gr = new GlideRecord('table_name');
gr.addEncodedQuery('paste_encoded_query_here');
gr.query();
gs.info("***INFO: Deleting these many records: " + gr.getRowCount());
gr.deleteMultiple();

And then you can go to your table list view of these records, filter them exactly how you want, then copy the last piece of the breadcrumb trail and right-click and choose: "copy query".

find_real_file.png

Then paste that above where I've indicated. Then of course change it to your table name as well.

Please mark reply as Helpful/Correct, if applicable. Thanks!


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

Hi,

The first reply you had for this post had errors, which the user has come back and changed it...thus I wouldn't have even posted, but anyways...

If I've helped answer your question...as well as give instruction on how to learn a neat trick for encodedQueries in the future.

Please mark my reply above as Helpful/Correct.

Take care!


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

Community Alums
Not applicable

It worked. Thanks.