Activity Forums Salesforce® Discussions How to display validation error in same page using Salesforce trigger on click of delete button?

  • Roopam K

    Member
    June 14, 2019 at 8:26 am

    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 4 years, 9 months ago by  Roopam K.
  • Shakti Sharma

    Member
    June 14, 2019 at 9:10 am

    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:

    1. 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.
    2. 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.

Popular Salesforce Blogs

Popular Salesforce Videos