Org Limits Lightning component

In Summer 19, Salesforce introduced a new class named OrgLimit under the System namespace. The System.OrgLimit class contains methods that return the name, current value, and maximum limit for an instance.

By utilizing information from this class, I have created a lightning component that will be useful for the Org admin and developers. This component lists down all the org limit, its current usage and the maximum limit.

The component gets the org limits by making use of the OrgLimit class and displays the data in a tabular format. You can also refresh the component by clicking on the Refresh icon present at the top right corner. The refresh icon also displays the time when the component was last refreshed.


Below is the code for the lightning component:

OrgLimits.cmp
<aura:component implements="flexipage:availableForAllPageTypes" controller="OrgLimitsCtrl">
 
    <!-- Attributes -->
    <aura:attribute name="orgLimitInfo" type="List"/>
    <aura:attribute name="lastRefreshedDate" type="DateTime" />
    <aura:attribute name="timeZone" type="String" />
    <!-- Event Handlers -->
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
 
    <lightning:card title="Org Limits" iconName="standard:work_capacity_limit">
        <aura:set attribute="actions">
            As of
            <lightning:formattedDateTime value="{!v.lastRefreshedDate}"
                                         month="numeric"
                                         day="numeric" hour="2-digit"
                                         minute="2-digit" timeZone="{!v.timeZone}"
                                         hour12="true"/>
            <button name="Refresh" class="slds-button">
                <span data-value="Refresh" onclick="{!c.doInit}">
                    <lightning:icon iconName="action:refresh" size="xx-small"/>
                </span>
            </button>
        </aura:set>
        <table class="slds-table slds-table_cell-buffer slds-table_bordered slds-table_striped">
            <thead>
                <tr class="slds-line-height_reset">
                    <th class="" scope="col">
                        <div class="slds-truncate" title="Limit Name">Limit Name</div>
                    </th>
                    <th class="" scope="col">
                        <div class="slds-truncate" title="Used">Used</div>
                    </th>
                    <th class="" scope="col">
                        <div class="slds-truncate" title="Available">Available</div>
                    </th>
                </tr>
            </thead>
            <tbody>
                <aura:iteration items="{!v.orgLimitInfo}" var="item">
                    <tr class="slds-hint-parent">
                        <td data-label="Limit Name">
                            <div class="slds-truncate" title="{!item.limitName}">
                                {!item.limitName}
                            </div>
                        </td>
                        <td data-label="Limit Name">
                            <div class="slds-truncate" title="{!item.usedLimit}">
                                {!item.usedLimit}
                            </div>
                        </td>
                        <td data-label="Limit Name">
                            <div class="slds-truncate" title="{!item.maxLimit}">
                                {!item.maxLimit}
                            </div>
                        </td>
                    </tr>
                </aura:iteration>
            </tbody>
        </table>
    </lightning:card>
</aura:component>
OrgLimitsController.js
({
doInit : function(component, event, helper) {
        component.set("v.lastRefreshedDate", new Date());
        component.set("v.timeZone",$A.get("$Locale.timezone"));
helper.fetchOrgLimits(component);
}
})
OrgLimitsHelper.js
({
fetchOrgLimits : function(component) {
        var action = component.get("c.getOrgLimits");
        // Create a callback that is executed after
        // the server-side action returns
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var result = response.getReturnValue();
                component.set("v.orgLimitInfo", result);
            } else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " +
                                 errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
        });
        $A.enqueueAction(action);
}
})

OrgLimitsCtrl
public class OrgLimitsCtrl {

    @AuraEnabled
    public static List<OrgLimitInfo> getOrgLimits(){
        Map<String,System.OrgLimit> limitsMap = OrgLimits.getMap();
        List<OrgLimitInfo> orgLimitInfoList = new List<OrgLimitInfo>();
        for(String limitName:limitsMap.keySet()) {
            System.OrgLimit orgLimit = limitsMap.get(limitName);
            orgLimitInfoList.add(new OrgLimitInfo(limitName, orgLimit.getValue(), orgLimit.getLimit()));
        }
        return orgLimitInfoList;
  }
   
    public class OrgLimitInfo{
     
        @AuraEnabled
        public String limitName {get;set;}
        @AuraEnabled
        public Integer usedLimit {get;set;}
        @AuraEnabled
        public Integer maxLimit {get;set;}
       
        public OrgLimitInfo(String limitName, Integer usedLimit, Integer maxLimit){
            this.limitName = limitName;
            this.usedLimit = usedLimit;
            this.maxLimit = maxLimit;
        }
    }

}


Comments

Popular posts from this blog

Salesforce Lightning: Countdown timer

Salesforce Hacks: System.LimitException: Too many queueable jobs added to the queue: 2

Building an Org Role Hierarchy component in LWC