- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2016 01:18 AM
Hi All,
I need to delete certain records in a table where a particular field has no value.
I have done it for single record using the below code.
var gr = new GlideRecord('table_name');
gr.addQuery('sys_id','07dbd0ea4f005200634df5a18110c75c');
gr.query();
gr.next();
gr.deleteRecord();
How can i fetch multiple records and delete them using Scheduled jobs.
Thanks,
Ramya
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2016 01:27 AM
var gr = new GlideRecord('table_name');
gr.addNullQuery('fieldName');
gr.deleteMultiple();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2016 01:23 AM
You can use deleteMultiple() instead of doing it one by one. Just make sure the query returns all the records that need to be deleted, and then execute this command. Look at this wiki for an example.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2016 01:44 AM
Thanks Van for your help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2016 01:26 AM
Ramya,
If you are deleting multiple records then the 'deleteMultiple' method can be used as a shortcut,
Here is a sample to delete all inactive incident :
//Find all inactive incidents and delete them all at once
var gr = new GlideRecord('incident');
gr.addQuery('active', false);
gr.deleteMultiple(); //Deletes all records in the record set
if you would like to use a GlideRecord, here is a sample of script which deletes inactive incident (as above) :
var gr = new GlideRecord('incident');
gr.addQuery('active',false);
gr.query();
while (gr.next()) {
//Delete each record in the query result set
gr.deleteRecord();
}
you can addQuery('fielname', '') to get specific field with no value and also delete them.
let me know if you have any concerns
Kind regards,
ZA
Do not feel shy to mark correct or helpful answer if it helps or is correct
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2016 01:44 AM
Thank you Zic.