1

in apex, can it be handled so that when the program runs an error and the catch fires an exception, can this exception do 3 things at the same time? (I used platform events and it only gives 1 and 2 but not 3)

 1. throw new AuraHandledException(e.getMessage()) => Stop the program immediately
 2. Insert errors Log to the customLog object
 3. Send error email

And if it can be done, how to do it? Thank you so much!

1
  • Hi, and welcome to SFSE! If you haven't, please take a moment to read about How to Ask and take our tour. Your question is mostly fine, I just wanted to point out that gratitude is implied and shouldn't be part of the post (e.g. "Thank You Very Much", "Thanks in Advance", etc). If you want to, you can tidy up your answer a bit with an edit. Other than that, I hope you enjoy your time here.
    – sfdcfox
    Commented Oct 22, 2022 at 5:06

1 Answer 1

1

As outlined in this question and accepted answer, in order to make the email send work, you have to specify an alternative address to send from, because the default use, Automated Process, doesn't have a valid email address, so those emails are dropped.

You can, however, send emails via email alert where the From Email Address is NOT "Current User's Email Address" but rather some other organization-wide email or default workflow user.

Since that answer was written, you can now Configure the User and Batch Size for Your Platform Event Trigger with PlatformEventSubscriberConfig. You should be able to set the Platform Event Trigger to a user with a valid address, and the emails should send just fine.


Alternatively, you can do what I've done in my projects. I throw the exception back to the client, as you have, then the client does the separate work of calling back to the server to log the error and do whatever else needs to be done (which, for your case, would be sending an email).


Or, you can choose to not throw an exception. After all, you control the data structures. I've had a project where every method would use some kind of variant of this class:

public class Response {
  @AuraEnabled public Boolean success { get; set; }
  @AuraEnabled public String response { get; set; }
}

Where success is set to false if I handle an error, and the response is whatever I need it to be. It might be a String, or a Map<String, Object>, or you could even build some more elaborate design using a custom class, perhaps with subclasses to make it easier to manage.


At any rate, you have at least a few options, though the first should probably work and require the least amount of effort.

1
  • I did it. That's working. Thank you so much @sfdcfox
    – David
    Commented Oct 22, 2022 at 7:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .