function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MycodexMycodex 

send email before adderror

I'm having trouble sending out an email message if I use an adderror. I've read in the apex docs that its not processing since the adderror does not allow the tranaction to commit. Knowing this is the case, what alternatives do I have?

 

Ultimately, I want to prevent a user from deleting a contact when there are licenses and I also want to be notified that the action has taken place. I think I might have to get creative for this. Here is the trigger I have so far:

 

 

trigger PreventDelete on Contact (before delete) {
for (License__c l : [select contact__c from license__c
where contact__c in :Trigger.oldMap.keySet()]) {

//Setup email and send
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'email@acme.com'};
mail.setToAddresses(toAddresses);
mail.setReplyTo('emaiL@acme.com');
mail.setSenderDisplayName('Salesforce Support');
mail.setSubject('Contact Deletion Attempted');
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setPlainTextBody('A contact delete was attempted.');
mail.setHtmlBody('Review contact for deletion<p>'+
' View license<a href=https://na5.salesforce.com/l.id>click here</a>');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

Trigger.oldMap.get(l.contact__c).addError('Contact has licenses attached. Support has been notified.');
}
}

 

 

 

email@acme
Ispita_NavatarIspita_Navatar

I tried your code in my org, made a slight modification changed line is high-lighted and code is given below:-


trigger PreventDelete on Position__c (before delete) 
{

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {'engg.of.cse@gmail.com'}; 
            mail.setToAddresses(toAddresses);
            mail.setReplyTo('engg.of.cse@gmail.com');
            mail.setSenderDisplayName('Salesforce Support');
            mail.setSubject('Contact Deletion Attempted');
            mail.setBccSender(false);
            mail.setUseSignature(false);
            mail.setPlainTextBody('A contact delete was attempted.');
            mail.setHtmlBody('Review contact for deletion<p>'+' View license<a href=https://na5.salesforce.com/l.id>click here</a>');
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            Trigger.old[0].addError('Contact has licenses attached. Support has been notified.');  // Changed code
}

It ran successfully without any error.

Still if it is imperative for you code as you have coded, you can check the "SendEmailResult"  object which is returned whenever send mail using Messaging.sendEmail, if it returns a success message then you use your adderror line.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

jonthepiratejonthepirate

Your example code doesn't work. When I set the to address to myself and run it, i don't get an email. Here's my code:

 

trigger blockNewTicketCreationWhenSourceIsEmail on Case (before insert) {

    // Pull the new case out of the triggered event....

    for (Case newCase : Trigger.new) {
            
             //Setup email and send
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {'iRemovedMyEmailForMyPrivacy'};
            mail.setToAddresses(toAddresses);
            mail.setReplyTo('emaiL@acme.com');
            mail.setSenderDisplayName('Salesforce abc testSupport');
            mail.setSubject('Contact Deletion Attempted');
            mail.setBccSender(false);
            mail.setUseSignature(false);
            mail.setPlainTextBody('A contact delete was attempted.');
            mail.setHtmlBody('Review contact for deletion<p> View license<a href=https://na5.salesforce.com/l.id>click here</a>');
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
             
            // Prevent the insert.
            newCase.Origin.addError('Cases may not be created from email. The trigger titled "blockNewTicketCreationWhenSourceIsEmail" has prevented this email from creating a new case. The customer has been sent an auto-reply explaining that they must go to the order status page to submit questions.');
      
        
        // Break out out of the loop I used to access the email message.
        break;
        
    }
    
}

MycodexMycodex

The adderror prevents all transactions from committing including emails. My code successfully builds the email but it just doesn't send because the adderror cancels the entire transaction. I haven't been able to figure it out 100% but I have to implement partial processing according to this page

 

http://wiki.developerforce.com/index.php/An_Introduction_to_Exception_Handling

 

The section of interest is under "How do I handle exceptions that I've caught?". The one drawback of this method is that you need to implement your own rollback.  Basically, I'd want to create a savepoint, perform my database change, check the results and email/adderror if the contact has any license records associated. This is a bit over my head at the moment, but if anyone wants to chime in then I'm all ears.

bhagya manshanibhagya manshani

plz help me out. its not Working