Br to prevent deletion

nafiz435
Tera Contributor

Can anyone share a business rule script example that prevents record deletion under certain conditions

1 ACCEPTED SOLUTION

Its_Azar
Tera Guru

Hi there @Yashsvi 

 

you can use an br like below

(function executeRule(current, previous /*null when async*/) {

    /
    if (current.u_status === 'Closed' && current.u_impact === 'High') {
        gs.addErrorMessage('Cannot delete record because it is closed with high impact.');
        current.setAbortAction(true); 
    }

})(current, previous);
☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.




Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG

 Microsoft MVP (AI Services), India

View solution in original post

5 REPLIES 5

Saloni Suthar
Mega Sage
Mega Sage

Hi @nafiz435 ,

The business rule would not work because the business rule will run the logic when the records are deleted. You have to add conditions to the delete ACL on the table. 


If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni

Yashsvi
Kilo Sage

Hi @nafiz435,

You could write a before delete business rule that queries associated tables to see if there is are records referencing current and then do a setAbortAction(true) if so.

please check below link:

https://www.servicenow.com/community/developer-forum/prevent-record-to-be-deleted-if-it-has-associat...

Thank you, please make helpful if you accept the solution. 

Tony Chatfield1
Kilo Patron

Hi, you can abort a BR action with a before Delete BR and setAbortAction(); But I would first carefully review the business drives\use case that require this, as I would not normally consider scripted control of record deletion to be best practice and normally records should be retained to preserve audit integrity.

if(current.operation() == 'delete') {
	current.setAbortAction(true);
}

 

 

Its_Azar
Tera Guru

Hi there @Yashsvi 

 

you can use an br like below

(function executeRule(current, previous /*null when async*/) {

    /
    if (current.u_status === 'Closed' && current.u_impact === 'High') {
        gs.addErrorMessage('Cannot delete record because it is closed with high impact.');
        current.setAbortAction(true); 
    }

})(current, previous);
☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.




Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG

 Microsoft MVP (AI Services), India