+ Start a Discussion
dave23dave23 

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).

 

Thanks

 

Dave

ErikOSnet0ErikOSnet0

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?

dave23dave23

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?

 

Thanks

ErikOSnet0ErikOSnet0

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. 

 

dave23dave23

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 (http://forums.sforce.com/sforce/board/message?board.id=practices&message.id=8788&query.id=221135#M8788) 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

 

Dave

dave23dave23

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?

 

Dave

ErikOSnet0ErikOSnet0

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 :
  1. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_forcecom_email_outbound.htm
  2. http://help.salesforce.com/HTViewSolution?id=000006765
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

Hello, 

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

https://help.salesforce.com/articleView?id=000330030&type=1&mode=1
 

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