0

I created a static method to send exception email to developer name exptonEmail(). I have two classes name Class A and Class B. I am using the exptonEmail method in both class's method in catch block. If both class methods are called in same execution context and if there is a exception occurred for both class's method then how the exptonEmail will be send or how many times the email will be sent. What will be the best Salesforce practice to reuse exptonEmail in different methods catch block.

1

The answer depends on what happens in the transaction where the call to expTonEmail occurs.

  • If the transaction ends in an uncaught exception, it doesn't matter how many times expTonEmail is called as no emails will be sent for the transaction.

  • If the calls to expTonEmail are within a savepoint-rollback block, the emails will not be sent.

  • If you are running in a sandbox where email deliverability is not All, the emails won't be sent.

Otherwise, you will get outgoing emails for every call to the method (provided you don't exceed Apex outbound email limits)

| improve this answer | |
0

It's good practice to have exception handling at a high level of your software architecture. There are situations where catching exceptions at a lower level is acceptable but I would not recommend it. Let the top level classes decide what to do with exceptions. If you have exceptions at a lower level for logging (which is acceptable) rethrow the exception. Catching exceptions at low levels in the code and sweeping the error under the rug should is the real exception. In your case having exceptions propagate to the top will allow you more flexibility in how many times you call that email method.

| improve this answer | |
-1

The email will be sent out as many times as that method executes. I would recommend reading documentation around apex error propagation (or java error propagation, as it's kind of the same).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.