Delete records on related lists

caliban
Tera Expert

Hi,

 

I'm looking for a way to delete records in related lists when a record is deleted.

If I have a record in Table A and this table has 2 related lists Table B and Table C.  On deleting the record in Table A I would like it to delete the associated records in tables B & C.

Say if was for a Conference booking (Table A) and the related lists were Conference Room (Table B) & Conference Fees (Table C)

I had looked to setup a Before Business Rule running on Table A from a similar question posted on the forum but that hasn't worked 

var conf = new GlideRecord("TABLE B");
conf.addQuery('referenceField that relates to TABLE A', current.sys_id);
conf.deleteMultiple();

Are there any other ways around this?

Many thanks

 

 

1 ACCEPTED SOLUTION

Anurag Tripathi
Mega Patron
Mega Patron

Try This:

 

var conf = new GlideRecord("TABLE B");   /g was in small case previously
conf.addQuery('referenceField that relates to TABLE A', current.sys_id);
conf.deleteMultiple();

 

 

Also id suggest to use setWorkflow(false) if you dont need BRs to run, would improve query performance.

-Anurag

View solution in original post

9 REPLIES 9

Anurag Tripathi
Mega Patron
Mega Patron

Try This:

 

var conf = new GlideRecord("TABLE B");   /g was in small case previously
conf.addQuery('referenceField that relates to TABLE A', current.sys_id);
conf.deleteMultiple();

 

 

Also id suggest to use setWorkflow(false) if you dont need BRs to run, would improve query performance.

-Anurag

Hi,

 

Thanks for the response.  I've updated the typo in original question (lower case g) thanks for pointing it out.

 

When I delete the record in table A it only deletes the accompanying record in table B, not table C.  Is it as simple as creating another Business Rule for table C?  Or can I get the required result into a single Business Rule?

In the same BR you can first delete items for table b and then for table c

 

Same Script just change table and field name

-Anurag

Rajesh Kannan G
ServiceNow Employee
ServiceNow Employee

Have you considered the cascade delete option. You can refer our documentation Configure cascade delete rules for more details.

 

  1. Navigate to a reference field on a form.
  2. Right-click the field label and select Configure Dictionary.
  3. Under Related Links, click Advanced view.
  4. Locate the Reference Specification section.
  5. In the Reference cascade rule field, select one of the following options.
    OptionDescription
    Clear or -- None --Deleting a record clears references (default option).
    Delete or CascadeDeleting a record also deletes all referencing records. For example, when a user record is deleted, any incidents assigned to the user are also deleted.
    Caution: Use this method with caution.
     

Thanks,

Rajesh