Tuesday, November 15, 2016

Keeping track of Data Storage in Salesforce

Use Case
As you know some Clients want to know whenever data storage percentage is greater than 90% then we can send out mail to respective members.


Solution
We should write this code in apex class implements Schedulable interface and send out to mail respective members .
Here is code to retrieve current data storage Threshold value .

        String sid =UserInfo.getSessionId();
        HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        req.setEndpoint('https://cs2.salesforce.com/services/data/v32.0/limits'); //Lable.URL
        req.setHeader('Authorization', 'OAuth '+ sid);
        Http http = new Http();
        HTTPResponse res = http.send(req);
        Map<String, Object> m = (Map<String,Object>)JSON.deserializeUntyped(res.getBody());
        Map<String, Object> dataStorage = (Map<String,Object>)m.get('DataStorageMB');
        max= Integer.valueOf(dataStorage.get('Max'));
        remaing= Integer.valueOf(dataStorage.get('Remaining'));
        diff=max-remaing;
        threshold=max *0.9;
        DiffPer=(diff*90)/max;

No comments:

Post a Comment