Check Salesforce Org's Email Limits in Apex Programming

Since Salesforce works in a multi-tenant architecture, it is important to check org limits before executing any transaction. Specifically, Salesforce email limits in Apex become crucial when working in Service or Sales Cloud platforms, where sending many emails is essential for developing communication with customers.

Apex code to get the org email limits to avoid the following error.

System.EmailException: SendEmail failed. First exception on row X; first error: SINGLE_EMAIL_LIMIT_EXCEEDED, Email limit exceeded

Use the below code to check and get the limit before sending emails.

Map< String, System.OrgLimit> limitsMap = OrgLimits.getMap();
for(System.OrgLimit limit : limitsMap.values()) {
    if(limit.getName().equals('DailyWorkflowEmails')) {
        System.debug('Limit: ' + limit.getName());
        System.debug('Max Limit is: ' + limit.getLimit());
        System.debug('Usage: ' + limit.getValue());
    }
}

Hope this helps you avoid hitting governor limits while sending emails.

Thanks,

Sharath Chandra Varkole

Responses

Popular Salesforce Blogs