18 April 2015

Rollout your own limit dashboard




Spring 15 release brings some goodies as usual. One particular feature I liked is the REST API Limit Resource feature which will be in GA and will be available 24 hours after the scheduled release. I particularly like this feature, which I even sign up for a developer a preview a couple of months back, is because I'm annoyed with the system lack of dashboard page that allows me to track whether I'm almost hitting organisation limits. Simple requirement such knowing how many mail SingleEmail have been sent out requires you to contact Salesforce contact centre. Now it might be a product strategy from the San Francisco company as some of these limits (soft limits) can be increase by buying it in addition to the editions hard limits, or maybe just really not in the roadmap yet. We don't really know for sure but its good that developers and in some ways, administrators now has a way to check these limits without calling support.

Here are the limit information the the feature currently returns:
  • Daily API calls
  • Daily Batch Apex and future method executions
  • Daily Bulk API calls
  • Daily Streaming API events
  • Streaming API concurrent clients
  • Daily generic streaming events (if generic streaming is enabled for your organization)
  • Daily number of mass emails that are sent to external email addresses by using Apex or Force.com APIs
  • Daily number of single emails that are sent to external email addresses by using Apex or Force.com APIs
  • Concurrent REST API requests for results of asynchronous report runs
  • Concurrent synchronous report runs via REST API
  • Hourly asynchronous report runs via REST API
  • Hourly synchronous report runs via REST API
  • Hourly dashboard refreshes via REST API
  • Hourly REST API requests for dashboard results
  • Hourly dashboard status requests via REST API
  • Daily workflow emails
  • Hourly workflow time triggers
How to normally check the limit usage

Before we delve into how we can expose the limit information, lets take a look first on how we can normally access it. I will use workbench here for the purpose of simplification but feel free to use any tool of your liking.


Basically, we just need to go to the REST Explorer section of Workbench. Then you type in then limit resource url /services/data/<version>/limits/, then choose the GET option and then click Execute button.


Note that this is only available for REST API version 29 and to users with "View Setup and Configuration" permission. The DataStorageMB and FileStorageMB limits will only show for users with if "Managed Users" permission. 

Rolling out your custom limit dashboard

Administrators wouldn't want to always execute a REST call just to see the limit usage for the environment. What is needed is a dashboard the shows the current usage versus the hard limit. Below is a simple Visual force page executing a JavaScript to retrieve the same limit information.


<apex:page>
    <script type="text/javascript">
        var xmlhttp = new XMLHttpRequest();
        var url = "/services/data/v32.0/limits/";

        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState == 4 && 
                xmlhttp.status == 200) {
                myFunction(xmlhttp.responseText);
            }
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.setRequestHeader("Authorization", 
                                 "Bearer {!$Api.Session_ID}");
        xmlhttp.send();

        function myFunction(response) {
            var res = JSON.parse(response);
            var d = document.getElementById('result');
            
            // iterate to the object keys 
            // and display the property values
            Object.keys(res).forEach(
                function(key,index) { 
                    //key = the name of the object key 
                    //index = the ordinal position of the key within the object 
                
                    d.innerHTML += "<strong>" + key + "</strong><br/>" + 
                                  "Max: " + res[key].Max + "<br/>" + 
                                  "Remaining: " + res[key].Remaining + 
                                  "<br/><br/>";
        
            });
        }
    </script>
    <div id="result">
    </div>
</apex:page>

I will not explain the code in detail but as a high level, we did an HTTP get using JavaScript XMLHttpRequest object to the REST url /services/data/<version>/limits/, passing our session Id to authenticate and then iterating over the list of information returned by the call.

The sample screenshot is below:


We can include a lot of of other improvements to the page like changing the look and feel so that it would look better and to retry retrieving the information on a timed interval. I hope this post have help anyone.

Happy coding!

1 comment: