How to Access Custom Label in Salesforce Apex?

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.

  1. On the setup page, go to the quick find box and search, then select Custom Labels.
How to create custom labels in Salesforce
  1. On the setup page of Salesforce Custom Labels, click the New Custom Label button.
New custom label in Salesforce
  1. 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.
Create custom label in Salesforce lightning

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.

  1. 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.

  1. Now, we will call the above method using the code below in the Apex anonymous window.
TextCustomLabel.getCustomLabel();
  1. 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.
Get custom label in Salesforce Apex Class

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.

  1. On the setup page of Salesforce Lightning, navigate to the Quick Find box and search for ‘Translation Language Settings‘.
Enable translation settings in Salesforce
  1. In the Translation Settings, click on the Enable button.
Translate Custom label in Salesforce Apex
  1. 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.
Translation languages in Salesforce Apex
  1. 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.
Add language for custom label in Salesforce Apex
  1. After this, you will see the added languages in Salesforce’s Translation Workbench.
Add language to translation workbench in Salesforce

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.

Translated custom label in Salesforce Apex

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:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.