Business Rule Delete

Avinash Dubey2
Tera Contributor

Create a Business Rule which will trigger when record is deleted in Incident table then delete a record in Customer Incident table for that incident.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Please use before delete business rule and update your logic as this

(function executeRule(current, previous) {

	var gr = new GlideRecord('u_customer_incidents'); // give your correct table name
	gr.addQuery('u_parent_inc', current.number); 
	gr.query();
	if (gr.next()) {
		gr.deleteRecord();
	}

})(current, previous);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

12 REPLIES 12

Kartik Sethi
Tera Guru
Tera Guru

Hi @Avinash Dubey 

 

If possible, please provide:

  1. Table Name: for Customer Incident (until you provide details, let's say: u_customer_incident)
  2. Reference Recorduntil you provide details
    1. On Incident Record that can be used to uniquely identify the relation (let's say: u_customer_incident) OR
    2. On Customer Incident Record that can be used to uniquely identify the relation (let's say: u_parent_incident)
  3. Business Rule:
    • Table: Incident
    • When: Before
    • Delete: True
    • Advanced: True
    • Condition: Customer Incident â–º is NOT Empty
    • Script:
      Using Point#2.1
      (function executeRule(current, previous /*null when async*/) {
      	var customerIncident = current.u_customer_incident.getRefRecord();
      	if(customerIncident) {
      		customerIncident.deleteRecord();
      	}
      })(current, previous);

      Using Point#2.2
      (function executeRule(current, previous /*null when async*/ ) {
          var customerIncidentGr = new GlideRecord('u_customer_incident');
          customerIncidentGr.addEncodedQuery('u_parent_incident=' + current.getValue('sys_id'));
          customerIncidentGr.query();
      	//While loop in case multiple record are to be deleted
      	//If loop in case first appearing record has to be deleted
          while (customerIncidentGr.next()) {
              customerIncident.deleteRecord();
          }
      })(current, previous);

Please try and let me know if this works for you.

 


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik

Hi @Avinash Dubey 

 

Did you get a chance to check whether my solution worked for you or not?

If yes and if my answer helped you out then please mark my answer as correct/helpful, so that other community members facing similar issues might get help.

Thanks and regards,

Kartik

MichelSamia
Tera Expert

If you don't see the option Delete, you need to check the 'Advanced' checkbox.

business rule for delete operation.png