Wednesday, October 3, 2018

How to get governor limits of current org using REST API Callout

This Post explains the how to get the governor limits of current org using REST API Callout.

Some Cases we need to check the Max limit value and remaining limits values of current org in the apex.
For Example
Single Email Limit Values, Max limit value of Single Emails of Current org(Based on Licences it will depend) and How many are already invoked in a day.
Check below link to get limits by using the workbench
https://www.salesforcecodecrack.com/2018/09/how-to-check-your-org-governor-limits.html

if you need to check limits in Apex code, this post may help for you

Note: 
your salesforce instance URL must be added in your org remote site settings.

Check below class
/*
purpose: This Class gives response based on resource name you entered.
Author: Shaik Nagajani
Date: 10/3/2018
*/

public class OrgREST {
    
    public static String retriveResult(String strResourceName) {
        String response;
        String strEndPonitURL = URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v43.0/' + strResourceName;
        if(String.isNotBlank(strResourceName)) {
            HttpRequest httpRequest = new HttpRequest();  
            httpRequest.setMethod('GET');   
            httpRequest.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());        
            httpRequest.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID()); 
            httpRequest.setEndpoint(strEndPonitURL); 
            
            try {  
                Http http = new Http();   
                HttpResponse httpResponse = http.send(httpRequest);  
                if (httpResponse.getStatusCode() == 200 ) {  
                    response = httpResponse.getBody();  
                } 
                else {  
                    throw new CalloutException(httpResponse.getBody());  
                }   
            } 
            catch(Exception ex) {  
                throw ex;  
            }  
        } 
        return response;
    }
}
Open Developer Console, Execute below code from the anonymous window
String response = OrgREST.retriveResult('limits');
above class returns the response,
the response looks like(the small part of response) below.

"HourlyODataCallout" : {
    "Max" : 1000,
    "Remaining" : 1000
  },
  "HourlySyncReportRuns" : {
    "Max" : 500,
    "Remaining" : 500
  },
  "HourlyTimeBasedWorkflow" : {
    "Max" : 50,
    "Remaining" : 50
  },
  "MassEmail" : {
    "Max" : 10,
    "Remaining" : 10
  },
  "Package2VersionCreates" : {
    "Max" : 50,
    "Remaining" : 50
  },
  "PermissionSets" : {
    "Max" : 1500,
    "Remaining" : 1497,
    "CreateCustom" : {
      "Max" : 1000,
      "Remaining" : 997
    }
  },
  "SingleEmail" : {
    "Max" : 15,
    "Remaining" : 15
  },

Go to developer console, open execute anonymous window, copy below code and past in anonymous window
Integer iMax = 0;
Integer iRemaining = 0;
// Sending Request to the Server
String response = OrgREST.retriveResult('limits');

// deserializing the response
map<String, Object> mapJsonData = (map<String, Object>)JSON.deserializeUntyped(response);

// getting single Email max limit value and Remaining limit value 
map<String, Object> mapLimitValues = (map<String, Object>)mapJsonData.get('SingleEmail');
iMax = (Integer)mapLimitValues.get('Max');
iRemaining = (Integer)mapLimitValues.get('Remaining');

System.debug('Single Email Max Limit Vlaue====> '+iMax);
System.debug('Single Email Remaining Limit Vlaue====> '+iRemaining);

map<String, Object> mapLimitValues1 = (map<String, Object>)mapJsonData.get('DailyWorkflowEmails');
iMax = (Integer)mapLimitValues1.get('Max');
iRemaining = (Integer)mapLimitValues1.get('Remaining');

System.debug('DailyWorkflowEmails Max Limit Vlaue====> '+iMax);
System.debug('DailyWorkflowEmails Remaining Limit Vlaue====> '+iRemaining);
from above code, I checked single Email Max Limit, Remaining Limit Value and DailyworkfowsEmails invocation limit values.
Check below screenshot for more information about the result.

Output:

Based on Your Requirement, get the max limit and remaining limit values.
Let us Know if you have any queries.
Happy Learning!!!

No comments:

Post a Comment