You need to sign in to do that
Don't have an account?

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
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?
Thanks
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 (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
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
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.
After Transaction failure even future method is not executed, So you can't even create asynchronous job to send email.
Reference Links :
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
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
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