3

What is the easiest way to get a custom label from outside the platform (eg: node js or other app)?

I'm using nforce, but surprisingly don't see any api methods to get custom labels.

4

A quick glance at the nforce source makes it appear to not have support for fetching custom labels.

I can't see a way to fetch custom labels via the REST API either, nor can I find any references to custom labels in the SOAP API. Based on that I don't believe labels are readily useable off-platform without something like a custom apex REST/SOAP service (although there are complications as you can't fetch labels dynamically in apex) or by processing the CustomLabels.labels XML file from the metadata API.

However I have to ask you to clarify, do you mean the custom labels defined in the Setup > Create > Custom Labels menu, or the user friendly text for things like SObject fields?

| improve this answer | |
  • Yes I'm talking about the Setup>Create>Custom Labels – NSjonas Mar 12 '14 at 16:58
3

I can't believe they still haven't added this to the rest API. You can now access classes and tons of other meta-data but still no custom labels.

I've written a custom REST resource to allow this (as sfdcfox suggested). It uses the VF page getContent() hack which can be explained in more detail here.

VisualForce Page:

<apex:page controller="DynamicCustomLabel" contentType="application/json" showHeader="false" sidebar="false"
    language="{!lang}" standardStylesheets="false">{!$Label[name]}</apex:page>

Page Controller:

public class DynamicCustomLabel {
    public String lang {get;set;}
    public String name {get;set;}

    public  DynamicCustomLabel(){
       Map<String, String> reqParams = ApexPages.currentPage().getParameters();
       lang = reqParams.get('lang');
       name = reqParams.get('name');
    }
}

Rest Resource:

@RestResource(urlMapping='/customlabel')
global with sharing class DynamicCustomLabelResource  {

    @HttpGet
    global static String getLabel() {
        RestRequest req = RestContext.request;
        String name = req.params.get('name');
        String lang = req.params.get('lang');
        try{
            return getLabel(name, lang);
        }catch(Exception e){
            RestContext.response.statusCode = 404;
            return '';
        }
    }

    public static  String getLabel(String labelName, String language){
        Pagereference r = Page.DynamicCustomLabel;
        r.getParameters().put('lang', language);
        r.getParameters().put('name', labelName);
        String labelValue = r.getContent().toString();
        return labelValue;
    }
}

Calling it:

[HOST]/services/apexrest/customlabel?name=MyCustomLabel [HOST]/services/apexrest/customlabel?name=MyCustomLabel&lang=EN

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.