script include delete record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2022 07:47 PM
I want to delete the records in the table whose system name is the same as the status of abolished,
Executed when the next record is created。Urgently need your help
var CommonInfo_Grr = new GlideRecord('table');
//CommonInfo_Grr.initialize();
CommonInfo_Grr.addQuery("status", "abolished");
CommonInfo_Grr.addQuery("system_name",current.getValue("system_name"));
CommonInfo_Grr.query();
while(CommonInfo_Grr.next()) {
CommonInfo_Grr.addQuery("status", "abolished");
CommonInfo_Grr.deleteMultiple();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2022 04:56 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2022 05:11 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2022 09:28 PM
Hello
This is same question to this Question but still updating the response here as well. Please let me know that I got your requirement correctly or if you are asking for anything else:
I am not 100% clear with your requirement but as per the posted question 1 posibility of your requirement can be as mentioned below
1. There are some records in your table whose status is abolished and now you need to query these records get their system name (say you get Record1).
2. Once you get the System name of an abolished record then you again need to query your table to get the records whose System name is same as System name of the Record1 you got in step 1.
3. Now you need to delete the records you got in step 2.
If this is your requirement then you can use the below script:
(function () {
var CommonInfo_Grr = new GlideRecord("<YOUR_TABLE_NAME>");
CommonInfo_Grr.addQuery("status", "abolished");
CommonInfo_Grr.query();
while(CommonInfo_Grr.next()) {
var CommonInfo_Grr1 = new GlideRecord("<YOUR_TABLE_NAME>");
CommonInfo_Grr1.addQuery("system_name", CommonInfo_Grr.getValue("system_name"));
CommonInfo_Grr1.query();
while (CommonInfo_Grr1.next()) {
CommonInfo_Grr1.deleteRecord();
}
}
})();
You can also first get System name for all the records whose status is abolished and then query the table again to get the records with those System name. If your table has a lot of record that you need to delete then the below script will be more efficient:
(function () {
var abolishedRecords = []
var CommonInfo_Grr = new GlideRecord('<YOUR_TABLE_NAME>');
CommonInfo_Grr.addQuery("status", "abolished");
CommonInfo_Grr.query();
while(CommonInfo_Grr.next()) {
abolishedRecords.push(CommonInfo_Grr.getValue("system_name"));
if ((abolishedRecords.length) % 100 == 0) {
var CommonInfo_Grr1 = new GlideRecord('<YOUR_TABLE_NAME>');
CommonInfo_Grr1.addEncodedQuery("system_nameIN", abolishedRecords.join(",");
CommonInfo_Grr1.query();
while (CommonInfo_Grr1.next()) {
CommonInfo_Grr1.deleteRecord();
}
abolishedRecords = [];
} else {
continue;
}
}
})();
Please mark my response as helpful/correct, if it answer your question.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2022 10:51 PM
I tried your method, he deleted all records whose status is abolished, no filter system name
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2022 11:03 PM
I tried your method, he deleted all records whose status is abolished, no filter system name