4

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'];
1

1 Answer 1

9

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
    Commented Jul 16, 2017 at 13:15
  • @sfdcfox great to know, will keep in mind
    – RedDevil
    Commented Jul 16, 2017 at 13:57
  • 6
    This doesn't work any longer. You get an error it is restricted to VF Controllers being called from a VF page. Commented Mar 21, 2018 at 19:32

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