Sign up ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question
    
Are you referring to the addError () method on sObject? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – mkorman yesterday
    
also, can you post your full code to help everybody help you better? – mkorman yesterday
    
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'); } } } – sukit yesterday

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.

share|improve this answer

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

share|improve this answer

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

share|improve this answer
    
nop this will redirect to error page. – Ratan yesterday
    
is your trigger on before delete ? looks like you are using update trigger not delete. – Ratan yesterday
    
Hmm...ya its on before update....I believed it would had worked on before delete too. – Ruchi yesterday
    
No it is not working for before Delete .Its redirecting to new Error page – sukit yesterday
    
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'); } } } – sukit yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.