5

There is a way to get Custom Label statically in APEX as documented here

System.Label.Label_Name

Can I get this label without hardcoding Label_Name?

What I've tried so far (and did not work)

System.Label.get('Label_Name');
System.Label['elfBCLabel_VAT'];
0
7

You cant get dynamic custom labels in Apex:

Check out this idea

check out this link has a workaround solution which you can try in VF context:

 public static String getLabelString(String labelName ){
    Component.Apex.OutputText output = new Component.Apex.OutputText();
    output.expressions.value = '{!$Label.' + labelName + '}';
    return String.valueOf(output.value);
}
3
  • 2
    Just be prepared for an Internal Server Error if you try to get a label that doesn't exist... the resulting error is not catchable/blockable.
    – sfdcfox
    Jul 16 '17 at 13:15
  • @sfdcfox great to know, will keep in mind
    – RedDevil
    Jul 16 '17 at 13:57
  • 3
    This doesn't work any longer. You get an error it is restricted to VF Controllers being called from a VF page. Mar 21 '18 at 19:32

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