top of page

Simplifying Error Handling with Improved Apex Exception Emails

Writer's picture: S. YadavS. Yadav


Ever encountered an unexpected error in your Salesforce org and thought, "If only I had more details, I could fix this faster!"? You’re not alone!


Salesforce’s Apex Exception Emails are a lifesaver when it comes to error handling. They notify developers when an unhandled exception occurs, providing crucial debugging insights. But here’s the catch—the default emails often lack details. What if you could customize them to include everything you need—the exact method where the error happened, which user triggered it, and even the parameter values? Sounds awesome, right? Let’s dive in! 


In this blog, we will explore how to use Apex Exception Emails for better error handling and why customizing these notifications can save time and effort in troubleshooting, improving both application reliability and performance.


What Are Apex Exception Emails?

Apex Exception Emails are notifications sent by Salesforce when an unhandled exception occurs in your Apex code. By default, Salesforce sends these emails to the LastModifiedBy user of the failing Apex class or trigger. This can be useful, but it lacks context and flexibility. If you want to enhance your error-handling system, you can customize Apex Exception Emails to include more detailed information.

These emails come into play in two scenarios:

  • Apex Warnings: These emails are sent when a class or trigger exceeds 50% of an Apex governor limit, warning you that you're approaching a resource threshold.

  • Apex Exceptions: These are sent when an unhandled exception occurs in an Apex class or trigger, providing immediate feedback on critical errors that may disrupt business processes.

By customizing Apex Exception Emails, you can specify who receives the emails, and what information is included, ensuring the right person is notified immediately when an issue arises.


Why Customize Apex Exception Emails?

While the standard exception emails are useful, they often lack enough detail for effective troubleshooting. The default emails include only basic information like:

  • The type of exception

  • A brief stack trace

However, to fully understand and resolve the issue, you may need more context such as:

  • The method by which the error occurred

  • The specific parameter values involved in the error

  • The user who triggered the error

  • The time and date the error occurred

Customizing Apex Exception Emails allows you to include this critical data, which can speed up the process of identifying and fixing the problem.


How to Customize Apex Exception Emails in Salesforce

You can easily configure and customize Apex Exception Emails in Salesforce by following these steps:


Configure Apex Exception Email Notifications

To set up Apex Exception Email notifications in Salesforce, follow these steps:

  • Go to Setup.

  • In the Quick Find box, type Apex Exception Email.

  • Select Apex Exception Email.

  • Add the emails of Salesforce users or non-Salesforce email addresses who should receive exception notifications.

  • Click Save.

  • Click Add Salesforce User 

  • Save it. You can also add non-Salesforce email  

  • And save it 

These email addresses will now receive notifications when any unhandled exception occurs in the Salesforce environment.


After that setup let's try a scenario where you simulate an unhandled exception and get exception mail.

We are writing a trigger when we create a new Account and an unhandled exception occurs and we get an exception email.


Implementation

.class

public class ErrorHandlingDemo {
    public static void simulateAccountInsertLimitError(List<Account> newAccounts) {
        AccountDomain.causeAccountInsertLimitException(newAccounts);
    }
}
public class AccountDomain {
   public static void causeAccountInsertLimitException(List<Account> accounts){
        Account acc = new Account();
        insert acc;
    }
}

.trigger

trigger EmailException on Account (before insert) {
    ErrorHandlingDemo.simulateAccountInsertLimitError(Trigger.new);
}

Test Your Scenario

Open your Salesforce org from the App launcher and search for an account.

  • Open the account and create a new account 

  • Fill in all the mandatory fields and click on save.

  • When you click on save you get an error message.


  •   Now check your mail for sample output.


Sample Email Output:

Apex script unhandled trigger exception by user/organization: 0052w00000BpUnw/00D2w00000KsAHr

Organization: Avenoir (avenoir61-dev-ed.develop.my.salesforce.com).

CreateContactOnAccountAsCustomField: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0; 
	first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY,

Class.ErrorHandlingDemo.simulateLimitError: line 23, column 1 
 Trigger.EmailException: line 2, column 1

In case you didn’t get mail for output. You should check this configuration in your org. 

  • Open your org go to setup search for Process Automation setting  

  • Check for the ‘Send Process or Flow Error Email to’ dropdown and choose ‘Apex email exception Recipients’.


  • Next from set up search for  Deliverability 

  • Now check for Access to Send Email (All Email Services)

  • Access Level should be All Emails


Finally, you can check for user configuration for that 

  • From setup search for user 

  • Open the user and search for Send Apex Warning Emails it’s a check box that should be checked


Conclusion

Apex Exception Emails are a valuable tool in your error-handling arsenal. By customizing these emails, you can provide detailed information to help you and your team quickly identify and resolve issues in the Salesforce Apex code. This approach streamlines troubleshooting and reduces the time required to fix errors, ultimately improving the reliability and performance of your Salesforce environment.


By incorporating custom error handling and enhanced exception emails, you're not just reacting to issues—you’re proactively preventing them from becoming larger problems. This is a key step in ensuring your Salesforce applications run smoothly and your users are satisfied.


If you'd like to see the code and resources used in this project, you can access the repository on GitHub.To access the AVENOIRBLOGS repository, click here. Feel free to explore the code and use it as a reference for your own projects.


Happy Coding! You can leave a comment to help me understand how the blog helped you. If you need further assistance, please contact us. You can click "Reach Us" on the website and share the issue with me.


Reference


Blog Credit:

S. Yadav

Salesforce Developer

   Avenoir Technologies Pvt. Ltd.

  Reach us: team@avenoir.ai

Comments


© 2024 by Avenoir Technologies Pvt. Ltd.

bottom of page