1

We are using the salesforce API to consume picklist fields and display it on a webpage on a multilingual community "/services/data/v49.0/ui-api/object-info/Case/picklist-values/{recordtypeId}/customfield__c.

The problem is that I am not able to fetch the translated values. Can some let me know how can I get the translated values of the picklist. What changes needs to be made to the API call.

Following is the code.

public static List < pickVal > getPicklistValuesForRecordType(String objectType, String recordTypeName, String fieldName) {
    String recordTypeId = Schema.getGlobalDescribe().get(objectType).getDescribe().getRecordTypeInfosByDeveloperName().get(recordTypeName).getRecordTypeId();
    // Create the endpoint dynamically for your og
    String Endpoint = System.Url.getOrgDomainUrl().toExternalForm();
    
    endpoint += '/services/data/v49.0';
    endpoint += '/ui-api/object-info/' + objectType + '/picklist-values/' + recordTypeId + '/' + fieldName;
    
    EncodingUtil.urlEncode(endpoint, 'UTF-8');

    // Call the API in order to get the values 
    HttpRequest req = new HttpRequest();

    // Since, we can not use the UserInfo.getSessionId() method here, I am using a Visualforce page in order
    // to extract the session id which we can use for making API calls
    REST_Callout_Creds__c restCreds = REST_Callout_Creds__c.getOrgDefaults();
    req.setHeader('Authorization', 'OAuth ' + getSessionId(restCreds.Username__c, restCreds.Password__c));
    req.setHeader('Accept', 'application/json ');
    req.setHeader('Content-Type', 'application/json');

    req.setEndpoint(endpoint);
    req.setMethod('GET');
    Http http = new Http();

    HTTPResponse res = new HttpResponse();
    if (!Test.isRunningTest()) {
        res = http.send(req);
    } else {
        res.setStatus('OK');
        res.setStatusCode(200);
        res.setBody([SELECT Id, Body FROM StaticResource WHERE Name = 'httpResponsePicklist'
            LIMIT 1
        ].Body.toString());
    }
    List < pickVal > options = new List < pickVal > ();
    if (res.getStatus() == 'OK' && res.getStatusCode() == 200) {

        Map < String, Object > root = (Map < String, Object > ) JSON.deserializeUntyped(res.getBody());

        for (Object obj: (List < Object > ) root.get('values')) {
            Map < String, Object > mapObj = new Map < String, Object > ();
            mapObj = (Map < String, Object > ) obj;
            options.add(new pickVal(((String) mapObj.get('label')).unescapeHtml4(), ((String) mapObj.get('value')).unescapeHtml4()));
        }
    }
    return options;
}
1
  • I'm slightly confused. Why don't you simply combine the UI API results with the data you can get using the Apex Schema access?
    – Phil W
    Commented Feb 26, 2021 at 7:52

2 Answers 2

0

Your solution works but the API takes the language of the current user in context. You can also achieve the same result using the Schema Describe results but those methods also takes the language of the current user in context to get the label from the Schema.PicklistEntry.

0

There is not a clean solution, as Salesforce does not provide generic Apex methods for this. But this article describes a workaround that might be helpful: https://smartupload.medium.com/salesforce-tricks-how-to-retrieve-picklist-translations-in-any-language-programatically-1566fd91dc24

With this approach you don’t need the Metadata Api or UI Api at all.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .