How to write a fix script to delete multiple records from a table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2021 06:20 AM
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");
}
- Labels:
-
Script Debugger
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2021 06:22 AM
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();
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2021 06:24 AM
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".
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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2021 12:50 PM
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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2022 02:24 AM
It worked. Thanks.