Using Custom Label in Salesforce

Custom Labels are custom text values, up to 1,000 characters, that can be accessed from Apex Classes or Visualforce Pages. If Translation Workbench has been enabled for your organization, these labels can then be translated into any of the languages salesforce.com supports. This allows developers to create true multilingual apps by presenting information to users – for example, help text or error messages – in their native language. You can create up to 5,000 custom labels.

To access custom labels, from Setup, enter Custom Labels in the Quick Find box, then select Custom Labels.

Create Custom Labels

Create custom labels that can be referenced from Apex classes, Visualforce pages, or Lightning components to make an app multilingual.

NewLabel

Note – You can’t change the language of an existing custom label.
Using Custom Label in Apex and VFP –
  • In Apex use the System.Label.Label_name syntax.
  • In Visualforce and Lightning components, use the $Label global variable.

Translate Custom Labels

Translations for custom labels determine what text to display for the label’s value when a user’s default language is the translation language.

LabelTranslation

  1. In the Translations related list, click New to enter a new translation or Edit next to the language to change a translation.
  2. Select the Language you are translating into.
  3. Enter the Translation Text. This text overrides the value specified in the label’s Value field when a user’s default language is the translation language.

Using Custom Label in Apex and Visualforce Page –

Controller –


public class CustomLabelController {
public String labelValue{get;set;}
//Constructor
public CustomLabelController() {
labelValue = System.Label.Description;
}
}

Visualforce Page –


<apex:page controller="CustomLabelController">
<apex:form>
<apex:pageBlock>
<h1>Custom labels In VF Page :</h1>
<apex:outputLabel value="{!$Label.Description}"/>
<h2>Custom Label From Controller -:</h2>
{!labelValue}
</apex:pageBlock>
</apex:form>
</apex:page>

Below are the Pros and Cons of using Custom Label in Salesforce

Pros:

  • Code base is prepared for multi-lingual support.
  • Common error messages or help text can be reused
  • Ability to use a label in Apex Code and Visualforce Pages
  • Allows the text to be maintained by a system administrator rather than a developer
  • Data integrity is enforced if a delete attempt is made on the label and it is referenced by a Visualforce page or Apex Code
  • Readability of code and pages is improved by the use of custom label properties

Cons:

  • Salesforce.com instance must have the Translation Workbench enabled
  • Increased time to develop and coordinate with development team to ensure that custom labels are not used inappropriately
  • Limit of 1,000 characters and up to 5,000 custom labels. If developing for a large org you must take care to ensure the labels are manageable and utilized

One thought on “Using Custom Label in Salesforce

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s