- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2016 12:39 PM
Hi everyone, I'm quite new in ServiceNow so still learning.
I have a Business Rule that deletes child records from a parent table. I have added the code below but for some reason it wouldn't delete the entries. I'm probably missing something here..Any ideas?
var gr = new GlideRecord('table name');
gr.addQuery('u_parent', current.sys_id);
while (gr.next()) {
gr.deleteRecord();
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2016 01:24 PM
You are missing gr.query() here. Use this script and you are good to go
var gr = new GlideRecord('table name');
gr.addQuery('u_parent', current.sys_id);
gr.query();
while (gr.next()) {
gr.deleteRecord();
}
You can also use this to delete multiple records
var gr = new GlideRecord('table name');
gr.addQuery('u_parent', current.sys_id);
gr.deleteMultiple();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2016 12:45 PM
What is the table name that you are using? Is this part of a Scoped application? We need more detail as to what was set up in order for us to troubleshoot effectively. Thank you,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2016 12:52 PM
The code looks good. You are querying the records from child table that have current record as the value in u_parent field. If it is not deleting records, may be you need to check the conditions of the BR. Is the BR even triggering? Set up log statements to verify that it is kicking off and also to check that correct records are being captured.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2016 01:24 PM
You are missing gr.query() here. Use this script and you are good to go
var gr = new GlideRecord('table name');
gr.addQuery('u_parent', current.sys_id);
gr.query();
while (gr.next()) {
gr.deleteRecord();
}
You can also use this to delete multiple records
var gr = new GlideRecord('table name');
gr.addQuery('u_parent', current.sys_id);
gr.deleteMultiple();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2016 01:32 PM
You're absolutely right. Thanks Abhinay!