Skip to content

Instantly share code, notes, and snippets.

@Sunil02kumar
Last active April 19, 2019 07:54
Show Gist options
  • Save Sunil02kumar/e1a221f2a1263d094b170114f95b7056 to your computer and use it in GitHub Desktop.
Save Sunil02kumar/e1a221f2a1263d094b170114f95b7056 to your computer and use it in GitHub Desktop.
Create Update Custom Label by Using Metadata API
//Author- Sunil Kumar
public class MetadataAPIUtility{
public static boolean createCustomLabel(string csname,string description,string language,string value, boolean isProtected){
List<MetadataService.Metadata> allMetadataList = new List<MetadataService.Metadata>();
MetadataService.MetadataPort service = createService();
MetadataService.CustomLabel csLabel = new MetadataService.CustomLabel();
csLabel.fullName = csname;
csLabel.language = language;
csLabel.protected_x = isProtected;
csLabel.shortDescription = description;
csLabel.value=value;
allMetadataList.add(csLabel);
List<MetadataService.SaveResult> results = service.createMetadata(allMetadataList);
for(MetadataService.SaveResult result: results){
system.debug('update custom label results success='+result.success);
}
return results[0].success;
}
public static boolean updateCustomLabel(string csname,string description,string language,string value, boolean isProtected){
List<MetadataService.Metadata> allMetadataList = new List<MetadataService.Metadata>();
MetadataService.MetadataPort service = createService();
MetadataService.CustomLabel csLabel = new MetadataService.CustomLabel();
csLabel.fullName = csname;
csLabel.language = language;
csLabel.protected_x = isProtected;
csLabel.shortDescription = description;
csLabel.value=value;
allMetadataList.add(csLabel);
List<MetadataService.SaveResult> results = service.updateMetadata(allMetadataList);
for(MetadataService.SaveResult result: results){
system.debug('update custom label results success='+result.success);
}
return results[0].success;
}
private static MetadataService.MetadataPort createService(){
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();
return service;
}
/*execute below code in execute anonymous
MetadataAPIUtility.createCustomLabel('SFDC_Blog_URL','My test label from metadata api','en_US','http://www.sfdcstuff.com/',false);
MetadataAPIUtility.updateCustomLabel('SFDC_Blog_URL','My test label from metadata api','en_US','https://www.sfdcstuff.com/search/label/Lightning',false);
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment