Wednesday, July 4, 2018

Create Update Custom Label by Using Metadata API

We usually update custom labels from UI. After sandbox refresh, we update all custom labels so that these don't point to production URLs or values.

By using Metadata API, we can write automated apex script through which we can update all custom labels.

You can either download Metadata API WSDL from Salesforce and generate apex class by clicking on Generate from WSDL button.


Or you can download the MetadataService class from below link:

MetadataService.cls

I have created 2 separate static methods, one for creating new custom label and another for updating existing custom label. Below is class code:


Now by running below code in execute anonymous in developer console, you can create custom label:

MetadataAPIUtility.createCustomLabel('SFDC_Blog_URL','My test label from metadata api','en_US','http://www.sfdcstuff.com/',false);



You have to specify custom label values as a parameters in static method.

In order to update custom label, execute below code:

MetadataAPIUtility.updateCustomLabel('SFDC_Blog_URL','My test label from metadata api','en_US','https://www.sfdcstuff.com/search/label/Lightning',false);



Hope this will help!!




6 comments: