Tuesday 30 December 2014

Salesforce Organization File and Data Storage usage find out by using APEX

To find org file and data usage copy below class and save 


Step 1:


public class OrgFileSpaceLimit{

public void fileParseMethod(){

Datetime TDate = datetime.now();
String fileStorage = 'Used File Space</td><td class="dataCol">';
String dataStorage = 'Used Data Space</td><td class="dataCol">';
String endpointUrl = 'https://immu-dev-dev-ed.my.salesforce.com/00D90000000iCmb'; //-- endpointUrl = sales force instance/organization id-------

Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(endpointUrl);
req.setMethod('GET');
req.setHeader('Cookie','sid='+UserInfo.getSessionId());
HTTPResponse res = http.send(req);
String pageData = res.getBody();

//------Parsing the html body to fetch file and data usage and storing in a string-------------// 

String fileSize = pageData.substringAfter(fileStorage).left(5).remove('&n')+' MB ' + ' and percent used ' + pageData.substringAfter(fileStorage).substringBetween('(',')');
String DataSize = pageData.substringAfter(dataStorage).left(5).remove('&n')+ ' MB ' + ' and percent used ' + pageData.substringAfter(dataStorage).substringBetween('(',')');

//-----send email with out any condition-------------//
 
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new string[] {'skimran1038@gmail.com'});
mail.setSubject('Organization File and Data Storage Usage Space');
mail.setPlainTextBody('Total Used File Space by '+ TDate +' is '+ fileSize + '\n \n' + 'Total Used Data Space by ' + TDate + ' is ' + DataSize);
mail.setReplyTo('skimran1038@gmail.com');
mail.setSenderDisplayName(UserInfo.getUserEmail());
Messaging.SendEmailResult[] resp = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

Step 2:


Go to developer console or work bench executive below code or schedule the class

OrgFileSpaceLimit org = new OrgFileSpaceLimit();
    org.fileParseMethod();


No comments:

Post a Comment