function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jan4CJan4C 

How can I access Custom Labels from Apex code classes?

Hi,
in the Winter'09 release notes it says "Custom labels are custom text values, up to 1,000 characters in length, that can be accessed from Apex classes or Visualforce pages".
I could easily find how to access these labels from a visualforce page, but nowhere I can find how to access them from an apex class.
I need this because I have a class that dynamically generates labels for a certain table in a vf page. But I want to use Custom Labels (and the translation workbench) to get these translated in various languages.
Is there an object, something like Label with a static function like 'getLabel(String)'? Or is this not released in Winter '09?
Best Answer chosen by Admin (Salesforce Developers) 
AkiTAkiT
Found this from another post: (did not try it myself yet)

Code:
String test = Label.<custom label name>;

for example:

String test = Label.ok;

 

All Answers

AkiTAkiT
Anyone used custom labels in Apex class or trigger?

Cannot find any documentation either....
NikhilNikhil
I am also facing same problem,

Please suggest
AkiTAkiT
Found this from another post: (did not try it myself yet)

Code:
String test = Label.<custom label name>;

for example:

String test = Label.ok;

 

This was selected as the best answer
sdavidow9sdavidow9
I've tested this and it works.
Alexandre JaimesAlexandre Jaimes
is there a way to set what language the label will be translated? or is this only the current user. In my case I want to programatically send an email and set the language to the recipient, not the current user
nitesh gadkarinitesh gadkari
Documentation link for custom label 

https://help.salesforce.com/HTViewHelpDoc?id=cl_about.htm&language=en_US
Hugo CostaHugo Costa
Hi Guys,

I found a way to get the Custom Label String dynamically.

Check this method, it will return the string of the custom label by the string Name sent.

   // This method return the String value for the Label id
     public String getLabelString(String labelName ){
        Component.Apex.OutputText output = new Component.Apex.OutputText();
        output.expressions.value = '{!$Label.' + labelName + '}';
        
        return String.valueOf(output.value);
    }
sandeep@Salesforcesandeep@Salesforce
Simply you may use in apex Label.CustomLabel
Nagendra_SwamiNagendra_Swami
We can put label in salesforce these ways:
Visualforce Page: $Label.Label_API_Name

Lightning Component Page {!$Label.c.Label_API_Name}

Apex Class String getLabelName = Label.Label_API_Name;

or directly System.Label.Label_API_Name

hope this helps you.
Alderete_SFDCAlderete_SFDC
The answer provided by Nagendra_Swami is correct and covers Apex, Visualforce, and Lightning components.
 
We can access a custom label in Salesforce these ways:

Visualforce: {! $Label.Label_API_Name }

Lightning component: {!$Label.c.Label_API_Name}  
(Note: there are dynamic methods, too.)

Apex: String someLabel = System.Label.Label_API_Name;
 

Here's the relevant documentation for each:
F SmoakF Smoak
I am having a similar problem, could anyone please help me with my Question link posted? 
https://developer.salesforce.com/forums#!/feedtype=SINGLE_QUESTION_DETAIL&dc=General_Development&criteria=OPENQUESTIONS&id=9062I000000Xm11QAC
Antoine LeprinceAntoine Leprince
Hello, 
And where could I find a {!$Label.example} field in Salesforce to update it? 
Thanks !