Deleting a child record GlideRecord

reginabautista
Kilo Sage

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();

  }

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

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();


View solution in original post

6 REPLIES 6

ccajohnson
Kilo Sage

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,


veena_kvkk88
Mega Guru

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.


Abhinay Erra
Giga Sage

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();


You're absolutely right. Thanks Abhinay!