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
narendharnarendhar 

Is there anyway, We can send a email to admin from trigger in case if the trigger encounters add error method.

When ever user is trying to create duplicate contact ,I would like to send an email to Admin , . I am able to prevent the duplicate contacts using trigger , but not able to send email to Admin.

I need below two operation together.
1)User should receive the duplicate contact error.
2)Admin should receive an notofication email.
I am able to perform only one action not both , Any one has any solution for this scenario?
 
Wilfredo Morillo 20Wilfredo Morillo 20
Try ading this to your trigger or class:
*Change the subject and body of the email to your needs. You can also use a template if you want. 

reference: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm
//Get all Admin Users and add their emails to a list.
//Create an email to all admins and add it to the list.
//Send the email.  

List<User> adminUsers = new list<User>([Select email from user where profile.name = 'System Administrator']);
List<String> adminEmails = new List<String> ();
list<Messaging.SingleEmailMessage> notifications = new list<Messaging.SingleEmailMessage>();

    for (User adminUser :adminUsers ){
    
    adminEmails.add(adminUser.email);
    }


//Create a single email and add it to a list.
Messaging.SingleEmailMessage adminNotification = new Messaging.SingleEmailMessage();
adminNotification.setSubject('Your Subject');
adminNotification.setPlainTextBody('Your string Body');
adminNotification.setToAddresses(adminEmails);
adminNotification.saveAsActivity =  false;
        
notifications.add(adminNotification);

//Send the Email.
messaging.sendEmail(notifications);
Let me know if it worked for you.

Wil, 
narendharnarendhar
Thanks Will .

Either sending email or throwing an error using trigger.addError works , together not working.
 
Wilfredo Morillo 20Wilfredo Morillo 20
What error are you getting in the log?
narendharnarendhar
No error in logs , but as soon as the system encounters trigger.addError method entire business logic is getting rolled back.
I would like to send an email , when i encounter trigger.addError method.
I tried with email logic in @future method,Also i tried placing the Trigger.AddError method, Email logic in Finally{}block. None of the options are working here.
Tad Aalgaard 3Tad Aalgaard 3
You're never going to be able to do this.  If you google this you will find that it has been asked many times and none of them have had success.  The problem lies in the fact that emails are sent after the trigger runs without error.  Since it generates and error it won't get to the emailing.

https://developer.salesforce.com/forums/?id=906F00000008xNeIAI
narendharnarendhar
I Agree..!, i am searching for workaround solutions, But none of the options works here.