In Salesforce, Custom Labels support applications by providing text values that can be easily translated and managed. They help store static content, such as messages, instructions, and field labels.
In this Salesforce tutorial, I will explain custom labels in Salesforce Apex and how to create and access custom labels in Salesforce Apex.
What are Custom Labels in Salesforce Apex?
In Salesforce Apex, custom labels store the text that can be used in field labels or error messages accessed from Apex classes or Visualforce pages.
Custom Labels allow the automatic creation of multilingual applications in the user’s native language. They serve as placeholders for static text that may require frequent updates, translations, or customization without requiring modifications to the Apex code.
In Salesforce, we can create up to 5,000 custom labels, and the character limit is 1,000.
Create a Custom Label in Salesforce
To create a custom label, navigate to Salesforce’s setup page and follow the steps below.
- On the setup page, go to the quick find box and search, then select Custom Labels.

- On the setup page of Salesforce Custom Labels, click the New Custom Label button.

- Enter the required field: Short Description and Value. The Name field will be auto-filled according to the rented short description. At last, click on the Save button.

With this, the custom label will be created in your Salesforce instance.
Access the Custom Label in Salesforce Apex Class
In the steps below, we will see how to access custom labels and reference them in Apex classes. To do so, navigate to the Salesforce developer console and follow the steps below.
- Create a new Apex class and enter the code below.
public class TextCustomLabel {
public static void getCustomLabel() {
String s = System.label.Apex_Custom_Label;
System.debug('custom label is '+s);
}
}The method getCustomLabel has an expression, “String s = System.label.Apex_Custom_Label“, referring to the custom label we created.
- Now, we will call the above method using the code below in the Apex anonymous window.
TextCustomLabel.getCustomLabel();- In the Execution log window, select the checkbox to debug only, and then you will see the output as the text value fetched from the custom label.

This way, we fetch and refer to the custom label text values in Salesforce Apex classes by following the above steps.
Access translations of Custom Label in the Apex class
To translate the values of custom labels in Salesforce, we first need to set up the translation workbench.
In the setup, ensure that you add languages for translation, assign translators for each language, and activate or deactivate a language’s translations.
Now, follow the steps below to enable the translation workbench and add the language.
- On the setup page of Salesforce Lightning, navigate to the Quick Find box and search for ‘Translation Language Settings‘.

- In the Translation Settings, click on the Enable button.

- As we click the Enable button, the translation will be enabled. After this, we need to select the translation languages from the available options, which can be done by clicking the Add button.

- In this step, select the language and click on the Activate checkbox. We also need to select a translator for the chosen language.
- To do this, we will create user translators for this language, select them from the Available List, and click Add.
- At last, click on the Save button.

- After this, you will see the added languages in Salesforce’s Translation Workbench.

Get the translated Custom Label in Salesforce Apex Class
After enabling the translation settings, we will see how we can get the translated value of the Custom Label.
To get the translated value of the custom label, I have modified the Apex code as follows.
public class TranslatedCustomLabel {
public static void getCustomLabel() {
String textValue = System.Label.get('','Apex_Custom_Label','');
System.debug('Custom Label - '+textValue);
String textValueinGerman = System.Label.get('','Apex_Custom_Label','de');
System.debug('Custom Label in german - '+textValueinGerman);
}
}In the above apex code, we have used the expression “String textValueinGerman = System.Label.get(”,’Apex_Custom_Label,’ ‘de’),” in which the first parameter is for the namespace. The second is the label name, and the third parameter is for language. Through this, we will get a translation of the custom label into German.

This way, we can translate the custom label values and refer them to the Salesforce Apex code using the above code.
You may also like to read:
- Avoid Recursion in Salesforce Apex Triggers
- Salesforce Object Query Language (SOQL) in Apex
- Asynchronous Apex in Salesforce
- What is Schema Class in Salesforce Apex and How to Use it
- Restrict Record Access For Users in Salesforce
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.