Skip to content

Instantly share code, notes, and snippets.

@Sunil02kumar
Last active July 3, 2020 10:07
Show Gist options
  • Save Sunil02kumar/82ac1ca4a2965dc7ea91f6b5320340e7 to your computer and use it in GitHub Desktop.
Save Sunil02kumar/82ac1ca4a2965dc7ea91f6b5320340e7 to your computer and use it in GitHub Desktop.
Custom Label : Fetch all Custom Label Information through Execute Anonymous Script
public static string finalCSVString ='Id,Name,Value\n';
findAllCustomLabels('');
public static void findAllCustomLabels(string endpointURL){
if(endpointURL=='' || endpointURL==null){
endpointURL='/services/data/v45.0/tooling/query/?q=Select+id,Name,Value+from+CustomLabel';
}
HttpRequest req = new HttpRequest();
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
req.setHeader('Content-Type', 'application/json');
String domainUrl=URL.getSalesforceBaseUrl().toExternalForm();
system.debug('********domainUrl:'+domainUrl);
req.setEndpoint(domainUrl+endpointURL);
req.setMethod('GET');
Http h = new Http();
HttpResponse res = h.send(req);
string responseBody = res.getBody();
system.debug('****responseBody:'+responseBody);
JSONParser parser = JSON.createParser(responseBody );
string cname,cvalue,cid;
boolean isMoreRecordsAvailable;
string nextEndPointUrl;
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){
String fieldName = parser.getText();
parser.nextToken();
if(fieldName == 'done') {
isMoreRecordsAvailable=parser.getBooleanValue();
}if(fieldName == 'nextRecordsUrl') {
nextEndPointUrl=parser.getText();
}if(fieldName == 'Id') {
cid=parser.getText();
}if(fieldName == 'Name') {
cname=parser.getText();
}if(fieldName == 'Value') {
cvalue=parser.getText();
finalCSVString = finalCSVString + cid+','+cname.escapeCsv()+','+cvalue.escapeCsv()+'\n';
}
}
}
responseBody=null;
res=null;
system.debug('****Total Allowed Callout Limits-'+Limits.getLimitCallouts());
system.debug('****No.of callout performed-'+Limits.getCallouts());
system.debug('****Heap size used-'+Limits.getHeapSize());
if(!isMoreRecordsAvailable){
//add this condition if getting heap size error--6000000 heap size .
//&& (Limits.getLimitHeapSize()-Limits.getHeapSize()>100000)
if(Limits.getLimitCallouts()-Limits.getCallouts()>0 ){
findAllCustomLabels(nextEndPointUrl);
}
}else{
List<string> toAddresses= new List<string>();
toAddresses.add(UserInfo.getUserEmail());
sendEmail(finalCSVString,'All Custom Labels.csv','Custom Label Information through Developer console','Please find attached file for your reference',toAddresses);
}
}
public static void sendEmail(string csvString, string csvFileName,string emailSubject,string emailBody, List<string> toAddresses){
Messaging.EmailFileAttachment csvAtt = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(csvString);
string csvname= csvFileName;
csvAtt.setFileName(csvFileName);
csvAtt.setBody(csvBlob);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
if(toAddresses.size()==0){
string currentUserEmailId = UserInfo.getUserEmail();
toAddresses.add(currentUserEmailId);
}if(emailBody=='' || emailBody==null){
emailBody = 'Please find attached file for your reference.' ;
}
mail.setToAddresses(toAddresses);
mail.setSubject(emailSubject);
mail.setPlainTextBody(emailBody);
mail.setHtmlBody(emailBody);
mail.setFileAttachments(new Messaging.EmailFileAttachment[]{csvAtt});
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment