How to find Data Storage and File Storage using Salesforce Apex?

How to find Data Storage and File Storage using Salesforce Apex?

We can find Data Storage and File Storage using Salesforce Apex.

Option 1:

System.OrgLimit can be used to find Data and File Storage in MB.

Sample Code:

 Map < String, System.OrgLimit > limitsMap = OrgLimits.getMap();  
System.OrgLimit dataStorageLimit = limitsMap.get( 
    'DataStorageMB' 
);  
System.debug( 
    'Limit Name: ' + 
    dataStorageLimit.getName() 
);  
System.debug( 
    'Usage Value: ' + 
    dataStorageLimit.getValue() 
);  
System.debug( 
    'Maximum Limit: ' + 
    dataStorageLimit.getLimit() 
);  
System.OrgLimit fileStorageLimit = limitsMap.get( 
    'FileStorageMB' 
);  
System.debug( 
    'Limit Name: ' + 
    fileStorageLimit.getName() );  
System.debug( 
    'Usage Value: ' + 
    fileStorageLimit.getValue() 
);  
System.debug( 
    'Maximum Limit: ' + 
    fileStorageLimit.getLimit() 
);  

Option 2:

PageReference objPageRef = new PageReference(
    '/setup/org/orgstorageusage.jsp?id=' +
    UserInfo.getOrganizationId() +
    '&setupid=CompanyResourceDisk'
);
String strCompanyInfo = objPageRef.getContent().toString();
String strDataStorage = strCompanyInfo.substringBetween(
    'Data Storage',
    '</td></tr>'
).replaceAll(
    '&nbsp;',
    ' '
);
String strDataStoragePercent = strDataStorage.substring(
    strDataStorage.lastIndexOf( '>' ) + 1
);
System.debug(
    'Data Usage % is ' +
    strDataStoragePercent
);   
String strFileStorage = strCompanyInfo.substringBetween(
    'File Storage',
    '</td></tr>'
).replaceAll(
    '&nbsp;',
    ' '
);
String strFileStoragePercent = strFileStorage.substring(
    strFileStorage.lastIndexOf( '>' ) + 1
);
System.debug(
    'File Usage % is ' +
    strFileStoragePercent
);     

Use the above options to check the Data Storage and File Storage using Salesforce Apex.

Leave a Reply