How to access custom label in Apex

How to access custom label in Apex

In this blog, we will check how to access custom label in Apex. Custom labels are the text that can be accessed via Apex, VisualForce, LWC, aura, etc. It is a great way by which we can present our information in a multiple language.

What is a Custom Label?

Custom labels are mainly used to store the text that we can use in our field labels, error messages, etc without changing the Apex classes. Custom Labels allow the automatic creation of multilingual applications in the user’s native language. They act as placeholders for static text that might require frequent updates, translations, or customization without necessitating modifications to the Apex.

We can create up to 5000 custom labels in our org and 1000 characters can be provided in length.

Now let’s check how to create a custom label

  1. Navigate to Setup and search Custom label in the quick find box.
  2. Click on the new custom label.

Custom Label

  1. Provide a short description and value. Here, we can also provide a category that will help us to find our label, filtered in the list view.

So now we have created one Custom Label Test_Custom_Label.

View of Custom Label

How to access Custom Label in the Apex class?

Now let’s understand how can we reference custom labels in the Apex class.

Firstly, click on setup & open the developer console. As of now, we have created one Apex class DemoCustomLabel in which we have a method fetchCustomLabel.



developer console


Now, execute this class from the anonymous window by clicking on Debug –> Open Execute Anonymous Window –> Make sure to check the Open Log checkbox.

Anonymous window

Click on Execute and then open logs. Check on the Debug Only checkbox. We will get this below output “Custom label is Text coming from custom label”.

Debug logs


We also have another way of fetching Custom Labels. Let’s modify our above code .



Apex class modified


When we execute this class we will get the below output in our logs.



How to access translations of Custom Label in the Apex class?

To enable the translations for our custom label we have to first setup the translation workbench. Make sure to add languages for translation, assign translators for each language, and activate or deactivate a language’s translations. Follow the below steps to enable the translation workbench and add the language.

  1. Go to Setup and in the Quick Find box search for Translation WorkbenchTranslation Workbench
  2. Click on Enable and then Add the language from the drop-down and translator for that.

Here in our org, I have added German and English language and the same translator in both languages.

Language with translator

Now, let’s add the translations of our custom label concerning their language. To do that click on New then select the language from dropdown and the translated text.

Let’s modify our code again to fetch the translation.



In the above code, we have used the System.Label.get(”,’Test_Custom_Label’,’de’) in which the first parameter is for the namespace, as I don’t have a namespace connected with my current org, I have left it blank. The second is the label name and the third parameter is for language.

When we try to execute the code we will get the below output.

Translated custom label


What best practices can we follow while working with Custom Labels?

When working with Custom Labels, adhere to the following best practices:

  1. Descriptive Naming: Choose meaningful names for your Custom Labels to enhance readability and maintainability.
  2. Label Management: Regularly review and update Custom Labels to ensure they remain relevant and accurate.
  3. Localization: Leverage Custom Labels for multi-language support, allowing for seamless translation of text across different locales.
  4. Testing: Include tests to validate the behaviour of code that relies on Custom Labels, ensuring they function as expected.
  5. Governor Limits: Be mindful of Salesforce governor limits, especially when retrieving large numbers of Custom Labels in a single transaction.

Also Read – Future Method Scenario-Based Interview Questions in Salesforce


1. Can we perform an SOQL operation to fetch all the Custom labels in our application?

We cannot directly perform SOQL operation on Custom Labels but if we check the Use Tooling Api checkbox then we will able to fetch the data.


2. How to access custom labels in LWC?

To access custom labels in LWC we can use the below syntax to import it and then we can hold the value in some variable.

3. How to use Custom Label in SOQL query?

We can use the API Name of the Custom Label to reference it Below is the example, here we are fetching Account data filtered by Name where the value matches with the Custom Label value.


In this blog, we briefly explored the concept of Custom Labels and their versatile application across various domains. Custom Labels are integral to Salesforce development, providing a flexible solution for text resource management within applications.

Get a complete Roadmap To Learn Salesforce Admin And Development

Share Now

Kashish have extensive Salesforce development experience, holding 4 Salesforce certifications. she posses expertise in Apex, Lightning Web Components, and Salesforce Admin, with a track record of successful project delivery. As a dedicated Salesforce enthusiast, she actively seek and embrace new challenges and opportunities within the dynamic Salesforce ecosystem.

Similar Posts

One thought on “How to access custom label in Apex

Leave a Reply

Your email address will not be published. Required fields are marked *