Tuesday, January 8, 2019

How to check Org Limits values using Apex?

This post explains How to check org limits values using Apex.
in Spring'19 release salesforce introduces the System.OrgLimit and System.OrgLimits
System.OrgLimits getAll and getMap methods to obtain either a list or a map of all your org limits. To get details on each limit, use instance methods from System.OrgLimit.

if you want to check total org limits using Workbench check below post  https://www.salesforcecodecrack.com/2018/09/how-to-check-your-org-governor-limits.html
if you want to check total org limits using REST API check below link
https://www.salesforcecodecrack.com/2018/10/governor-limits-Using-REST-API.html

How to know Org Limit Names

if you want limit names to check values(max and usage values),  check below sample code it shows limit names.
execute below sample code from the anonymous window  
List<System.OrgLimit> limits = OrgLimits.getAll();
for (System.OrgLimit aLimit: limits) {
    System.debug('Limit Name  ' + aLimit.getName());
}
OR
List<System.OrgLimit> limits = OrgLimits.getAll();
for (System.OrgLimit aLimit: limits) {
    System.debug('Limit Name  ' + aLimit.getName());
}
Result

Getting the org limit values

to get the maximum value and the current value of an org limit.
use below sample code 
in this example, I am getting MassEmail limit and DailyWorkflowEmails limits, you can check any limit based on your requirement.
sample code
Map<String,System.OrgLimit> limitsMap = OrgLimits.getMap();
System.OrgLimit workFlowLimit = limitsMap.get('DailyWorkflowEmails');
System.OrgLimit massEmailLimit = limitsMap.get('MassEmail');

// Max and ussage limit values of workFlowLimit
System.debug('Usage Value: ' + workFlowLimit.getValue());
System.debug('Maximum Limit: ' + workFlowLimit.getLimit());

// Max and ussage limit values of MassEmail
System.debug('Usage Value: ' + massEmailLimit.getValue());
System.debug('Maximum Limit: ' + massEmailLimit.getLimit());
Result
Happy Learning!!!

No comments:

Post a Comment