Dynamically Access Custom Labels  in Apex

Dynamically Access Custom Labels in Apex

Last Updated on June 14, 2023 by Rakesh Gupta

Big Idea or Enduring Question:

  • How to access custom label in Apex dynamically?

Objectives:

After reading this blog, you’ll be able to:

  • Access custom label in Apex dynamically
  • Access custom label in Apex dynamically for a language
  • and much more

Business Use case

Ben Halpern is a Jr. Salesforce Developer at Gurukul On Cloud (GoC). Ben is writing Apex code for a user story that requires fetching the following custom label in German and Hindi. 

Automation Champion Approach (I-do):

After Summer’23 release you can use the System.Label.get(namespace, label, language) method to get a custom label, optionally specifying a language. The feature now allows dynamic resolution of label names at run time, including overriding the user’s current language if a translation exists for the requested language.

You can also check if translation exists for a label and language in a namespace by using Label.translationExists(namespace, label, language). You can’t access labels that are protected in a different namespace.

You can also use the System.Label.translationExists() method to determine if any translation is available for the custom label in another language. 

Guided Practice (We-do):

There are 3 steps to solve Ben’s business requirement using Apex and Custom Label. We must:

  1. Setup translation workbench
  2. Create custom labels
  3. Apex script to access custom labels dynamically

Step 1: Setup Translation Workbench

This step is optional; however, we must ensure that the translation workbench is correctly set up for current requirements. Read this help article for step-by-step instructions.

Make sure to add languages for translation, assign translators for each language, and activate or deactivate a language’s translations. I will add my user account to Hindi and German for the current business use case.

Step 2: Create Custom Labels

  1. Click Setup.
  2. In the Quick Find box, type Custom Labels.
  3. Select Custom Labels then click on the New Custom Label.
    1. Create the custom label as shown in the following screenshot:
  4. Click Execute.

Step 2: Apex Script to Access Custom Labels Dynamically

  1. Click on Setup | Developer Console. 
  2. I will be using Execute Anonymous Apex tool in the Developer Console to run the Apex code.
    1. An anonymous block is an Apex code that does not get stored in the metadata but, can be compiled and executed. Anonymous blocks execute as the current user and can fail to compile if the code violates the user’s object – and field-level – permissions.
  3. To open Execute Anonymous window, click on Debug | Open Execute Anonymous Window.
    
    
    String choiceInEnglish = System.Label.get('rakeshistomMVP','PreferredFoodChoice','');
    system.debug('Original Value-'+choiceInEnglish);
    
    String choiceInHindi = System.Label.get('rakeshistomMVP','PreferredFoodChoice','hi');
    system.debug('Hindi Value-'+choiceInHindi);
    
    String choiceInGerman = System.Label.get('rakeshistomMVP','PreferredFoodChoice','de');
    system.debug('German Value-'+choiceInGerman);

  4. To execute all codes in the window, click on the Execute button. Make sure to select Open Log check box. Once you do that, the log will automatically open in the Log Inspector after the code executes.
  5. The debug log will be listed on the Logs tab. Double-click the log to open it in the Log Inspector. In our case, the log will automatically open after the code executes. To better analyze the variables’ run time value, make sure to select the Debug Only check box, as shown in the following screenshot:

Formative Assessment:

I want to hear from you!

What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? Feel free to share in the comments below.

Have feedback, suggestions for posts, or need more information about Salesforce online training offered by me? Say hello, and leave a message!

2 thoughts on “Dynamically Access Custom Labels in Apex

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.