Search code examples
salesforcevisualforcesalesforce-lightningsalesforce-communities

How to get all custom labels information which is used in apex page?


I want to get all custom labels through rest API. I used custom labels in apex page like

{!$Label.MyCustomLabel}

. So can i get information about MyCustomLabel through rest api.

Any guidance would be appreciated. Thanks


Solution

  • There's not really an easy way to get the custom labels out of an org. The CustomLabel/ExternalString object isn't queryable, and the services/data/v43.0/nouns part of the rest api doesn't include them either.

    The only way to get custom labels from Salesforce right now is by reading metadata. The quickest way to do this would probably be to use the synchronous listMetadata and readMetadata calls. This uses the SOAP api, so there's a bit of XML involved here.

    1., listMetadata, replace org-id with your org id, and replace session-id with your session id.

    curl \
    -H 'Content-Type: text/xml' \
    -H 'SOAPAction: ""' \
    https://ap4.salesforce.com/services/Soap/m/38.0/org-id \
    -d '<?xml version="1.0" encoding="utf-8"?><env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><env:Header><n1:SessionHeader xmlns:n1="http://soap.sforce.com/2006/04/metadata"><n1:sessionId>session-id</n1:sessionId></n1:SessionHeader></env:Header><env:Body><n1:listMetadata xmlns:n1="http://soap.sforce.com/2006/04/metadata"><n1:queries><n1:type type="xsd:string">CustomLabel</n1:type></n1:queries></n1:listMetadata></env:Body></env:Envelope>'
    

    2., pull out all of the custom label names within the <fullName> tags

    3., readMetadata, replace org-id with your org id, replace session-id with your session id, and replace custom-label-name with the name of a custom label.

    curl \
    -H 'Content-Type: text/xml' \
    -H 'SOAPAction: ""' \
    https://ap4.salesforce.com/services/Soap/m/38.0/org-id \
    -d '<?xml version="1.0" encoding="utf-8"?><env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><env:Header><n1:SessionHeader xmlns:n1="http://soap.sforce.com/2006/04/metadata"><n1:sessionId>session-id</n1:sessionId></n1:SessionHeader></env:Header><env:Body><n1:readMetadata xmlns:n1="http://soap.sforce.com/2006/04/metadata"><n1:type type="xsd:string">CustomLabel</n1:type><n1:fullNames type="xsd:string">custom-label-name</n1:fullNames><n1:fullNames type="xsd:string">custom-label-name</n1:fullNames></n1:readMetadata></env:Body></env:Envelope>'