Activity › Forums › Salesforce® Discussions › How to display validation error in same page using Salesforce trigger on click of delete button?
Tagged: Data Validation, Delete, Salesforce Trigger, Validation Error
-
How to display validation error in same page using Salesforce trigger on click of delete button?
Posted by Manjunatha on June 6, 2019 at 5:30 PMHow to display validation error in same page without redirecting to new page using trigger on click of delete button?
Shakti Sharma replied 6 years, 11 months ago 3 Members · 2 Replies -
2 Replies
-
I am assuming you mean to show a validation error on the standard edit/detail page using a trigger on the deletion of the record. This can achieved by using the “AddError” method (refer to documentation here : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_exceptions.htm).
The trigger code would look something like this :
trigger <triggername> on <object> (before delete) { // iterate the deleted records from trigger.new for(<object> o : trigger.new) { if(<validation condition goes here>){ o.addError(<error message>); } }-
This reply was modified 6 years, 11 months ago by
Roopam K.
-
This reply was modified 6 years, 11 months ago by
- [adinserter block='9']
-
Hi Manjunatha,
As delete button redirects to standard delete URL, so any validation error at backend comes out like an unhandled exception and can be seen as a “not so friendly” error message.
Possible soluti0ns:
- Override delete button with a JavaScript button, using console api, query the record, check the delete condition as checked in validation rule and show the error message as a pop up from that JavaScript button. Pros: Easy, User-friendly. Cons: Not supported in lightning, no modifications in look and feel as standard browser specific alert message will be shown.
- Create URL button, a blank VF Page, or an Auro component, use that VF/Aura component’s controller to validate the record before deleti0n show an error message as a pop-up message if any error comes, redirect to the same record on click of okay. Pros: Can customize the error message in a way you want, using CSS. Cons: Development effort
Thanks
//Shakti
Log In to reply.