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

using addError and sending emails in Apex

Is there any way to add an error to an record being updated in a trigger and send an email. The trigger needs to send an email (one per batch). The email works fine if the processing generates no errors and I do not use the addError method. If I use the addError method to pass a message back to the user then the email is not sent.


So is there a way to send an email and use addError in the same trigger code? If not is there another mechanism that can be used to pass a message back to the user interface (standard salesforce UI).






I'm doing this in incoming email handling processing which don't have a web interface, but not triggers.  Yet, I suspect when an exception causes something to not happen that it is not being caught early enough.  In my case, I catch exceptions, add them to a List and then email out the list without a problem. 


Do you want to post the code where you catch the exception and send an email?


Thanks for the reply. I've narrowed down the problem a little further. When addError is called this prevents the database commit from occurring which also prevents the email from being sent. The requirements in this case are to give an error message to the user that the requested action could not be completed and to send an email with more details (not practical to display the detailed message in the UI). It doesn't matter whether I am catching exceptions in the trigger code or not.


So is there  a way around this limitation so that an email can be sent out in the same script that uses addError?




Check out the @future feature.  That results in a separate transaction/thread. 


If you can do this from the trigger, you can use it to send the email.  Sending an email is asynchronous anyway, as it goes into an SMTP queue for the Mail Transport Agent (aka, email server) to send later. 



I thought that the @future would still run under the same transaction but I tried it anyways and the email sent in the @future method is not sent if the code that calls the @future method uses addError. Others ( have also seen this as well.


I guess if there no way to do this all in Apex then I am left with adding the message to the record and using a workflow rule to send the email out. There is usally a Plan B!


Thanks for you suggestions




I wasn't thinking! Email still won't be sent from workflow rules if addError is called.


Surely there has to be someway to return a message to the user and send an email in the same transaction?




I'm a bit surprised that sending an email is within the same transaction.  Technically, it isn't transactional, per se, but the queueing of it might involve an insert, plus there is the creation of the audit data.  Typically, systems are designed to treat this as a separate transaction, particularly since its context is different (the email sending capability is a global service, not an instance service.) 


With the incoming email service where I'm having it reply with an email of the results of the operations, exceptions are caught and the primary transaction is rolled back (nothing is committed); but, the email goes out without a problem reporting the errors causing the rollback to the user. 


Perhpaps they did this better in incoming email services than they did in triggers.  


Lalit KishorLalit Kishor
Email does not sent until Apex Transaction is committed. 
After Transaction failure even future method is not executed, So you can't even create asynchronous job to send email.
User-added image
Reference Links :
Eduardo Offermann PalmaEduardo Offermann Palma
Hi, Is there any possibility to get acomplish the requirement of show a message and send email? Maybe through a workaround?
Leandro Fernandes 12Leandro Fernandes 12


You can create a Platform Event and fire a event. As events are always sent, even when the transaction fails afterwards, your Platform Event Apex Trigger will always execute.

So you just put your logic on the Platform Event Trigger

Tolga SunarTolga Sunar
Leandro Fernandes's solution is legit. I believe that, nowadays, if you require a trigger to both throw an addError and execute some logic, platform event is the only way to go. Tested, and works.
Neeraj Kumar VermaNeeraj Kumar Verma

normally when an exception occurs then the system shoots up an email to Admins to inform them about the exception, is anybody aware of how the system do that.

we can also use the same mechanism

Arabinda GhoshArabinda Ghosh

You can use the platform events to do that, I guess. The platform has the option to create it before the current transaction gets committed. 

Publish Immediately to have the event message published when the publish call executes. Select this option if you want the event message to be published regardless of whether the transaction succeeds

Aditya Yadav 16Aditya Yadav 16
Thanks @Leandro Fernandes, I was facing same issues and it resolved by using platform event as sugested by you