Pages

Tuesday, September 24, 2019

Custom Label : Fetch all Custom Label Information through Execute Anonymous Script

CustomLabel is available in Tooling API and can be used to get all CustomLabel Information. With single callout, REST API return 2000 records and then provide "nextRecordsUrl" attribute which contain URI for next set of custom label.

If you want to create class in your org and execute it methods to get custom label information, then refer below URL:

Custom Label : Fetch all Custom Label Information using Tooling API

But if you don't want to deploy new class to production or any org to get custom label information, then you can run below apex script in execute anonymous window in developer console to get custom label information.

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 });
}
view raw gistfile1.txt hosted with ❤ by GitHub
Hope this will help!!!

9 comments:

  1. Contact Quickbooks Technical Support Number 800-901-6679 to acquire instant solution of your issues & queries. Get round the clock assistance from our highly skilled technicians. Whenever appear with technical issues then make a call on the support anytime.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Custom food labels

    ReplyDelete
  4. A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. erinnerung

    ReplyDelete
  5. I am impressed. I don't think Ive met anyone who knows as much about this subject as you do. You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here. create photo with text

    ReplyDelete
  6. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. www.excellentcustomclothing.com

    ReplyDelete