5

Trigger.addError in before delete trigger always shows the error message in a new page. Instead of showing the error message in new page, I need to show in same standard page. Is it possible ?

And I have the condition to not delete the record when the active field is checked.

trigger OpportunityclosedWon on Opportunity (before Delete) {
    for (Opportunity opp : trigger.Old) {
        if (opp.StageName == 'Closed Won') {
            opp.stagename.adderror('You cannot delete Closed Won Opportunity');
        }
    }
}
4
  • Are you referring to the addError () method on sObject? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – mkorman
    Commented Dec 22, 2015 at 8:09
  • also, can you post your full code to help everybody help you better?
    – mkorman
    Commented Dec 22, 2015 at 8:38
  • Here is the code:- trigger OpportunityclosedWon on Opportunity (before Delete) { for(Opportunity opp:trigger.Old) { if(opp.StageName=='Closed Won') { opp.stagename.adderror('You cannot delete Closed Won Opportunity'); } } } Commented Dec 22, 2015 at 8:53
  • Pasted the code from above comment to question section for clarity in future. Commented Dec 16, 2020 at 3:35

3 Answers 3

3

https://developer.salesforce.com/forums/?id=906F000000090gBIAQ

Using standard page we can have these kind of messages only when we save info , but we cannot have this for delete actions as the page will be redirected to some predefined page. The only way is to customize the detail page using visual force and there we can have own custom validation.


Workaround

You can override Delete button with your javascript custom delete.

Check you condition in JAVASCRIPT and if satisfied then display alert message. else delete that record.

1

One of the possible solutions would be to override delete button -- create custom JS button with code like this:

var data = sforce.connection.query("SELECT Name, Id 
                                    FROM  Account 
                                    WHERE Id = '"+Account.Id+"'"
                                   );
var record = data[0];
if (record){
   var criteria = false;
   //check some criteria
   // if (record.Name = 'To Remove') criteria = true;
   if (criteria){
       result = sforce.connection.delete(record);
       if (!result[0].getBoolean("success")) alert("delete failed");
   }else{
       alert("Can not delete record because of criteria");
   }
}

I would recommend to put all criteria in the trigger because if record would be deleted from someone else -- criteria from JS would not work, and just handle error thrown by trigger and display in page.

If you don't like alert methods - you could use custom JS to display error on page - here are some examples: https://success.salesforce.com/answers?id=90630000000h9p6AAA.

What else can be done by JS - https://resources.docs.salesforce.com/sfdc/pdf/apex_ajax.pdf

0

Try adding the error to the field you want to show for. Example

Case.Status.adderror('Your error Message');

Here error message will be displayed on the Status field of the Case record.

Hope this helps.

Edit : I have done this thing in my trigger to add error message on status field via trigger if there is no Case Team Member present with the same role.

enter image description here

Regrds! Ruchi

10
  • nop this will redirect to error page.
    – Ratan Paul
    Commented Dec 22, 2015 at 8:10
  • is your trigger on before delete ? looks like you are using update trigger not delete.
    – Ratan Paul
    Commented Dec 22, 2015 at 8:18
  • Hmm...ya its on before update....I believed it would had worked on before delete too.
    – Ruchi
    Commented Dec 22, 2015 at 8:23
  • No it is not working for before Delete .Its redirecting to new Error page Commented Dec 22, 2015 at 8:24
  • trigger ChekTriggerOLD on Opportunity (before Delete) { for(Opportunity opp:trigger.Old) { if(opp.StageName=='Closed Won') { opp.stagename.adderror('You cannot delete Closed Won Opportunity'); } } } Commented Dec 22, 2015 at 8:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .