Wednesday, June 28, 2017

Custom Labels in salesforce :

Salesforce Custom Label

Custom Label maybe not so popular among Salesforce administrator compare to other feature, such as: workflow, formula field and so on. Custom Label enable developers to create multilingual applications by automatically presenting information in a user's native language from Apex class or Visualforce page.

Not only for developers, Custom Label actually also available for system administrators to be used in formula field. A value from a formula field can be depend on value from custom label referred, when display in page layout, it will be displayed based on language selected in user detail. Please note if you want to set Custom Label in multi-language, you need to enable Translation Workbench. To enable Translation Workbench, click Your Name | Setup | Administration Setup | Translation Workbench | Translation Settings and add languages to be supported.

Fetch the label from custom field label:
<apex:outputLabel value="{!$objecttype.account.fields.Acct_Recon_Full__c.label}"></apex:outputLabel>

If custom label is part of managed package, then we also need to use namespace for accessing custom label in our custom apex code which is not part of managed package. Lets say DemoLabel is part of managed package and managed package namespace is ‘myName’. Then we need to use following code to access DemoLabel in apex code.
1
String customLabelValue = System.Label.myName.DemoLabel;

To create Custom Label, click Your Name | Setup | App Setup | Create | Custom Labels. You only can add translation language for a custom label based on languages supported in Translation Workbench.


We can find Custom Label in Salesforce Global Variable start with $Label













And this is the final formula, it is just $Label.Label_Name








Fetch the Label Value using Salesforce Apex Class and Visualforce Page?


Custom labels are custom text values that can be accessed from Apex classes or Visualforce pages. Yes, we can fetch the Label Value both apex class and visualforce page

first create a Label using below path:

Setup-> App Setup-> Create-> Custom Labels
Create new label Called ‘Event Title’ and use the below code.

Apex Class:

String printLabel = Label.Event_Title;
system.debug(‘printLabel::’+printLabel);


Visualforce Page:

<apex:sectionHeader title=”{!$Label.Event_Title}” />


Note : we can create up to 5,000 custom labels and they can be up to 1,000 characters in length.

What are custom labels in Salesforce? What is the character limit of custom label?

Custom labels are custom text values that can be accessed from Apex classes or Visualforce pages. The values here can be translated into any language supported by Salesforce. 
Their benefit is that they enable developers to create multilingual applications which automatically presents information in a user’s native language.
You can create up to 5,000 custom labels for your organization, and they can be up to 1,000 characters in length.


How to Use Custom label in apex code?



How to Use  Custom label in apex code?

Custom labels are used to store custom text values which can be accessed in apex code or visualforce pages. It is also possible to translate these custom values in different languages. A custom label can hold up to 1000 characters in it and an org can have a total of 5000 custom labels.
You can Create a custom label by navigating to set up -> create -> custom label

In the below example let us see how we can access the custom label in apex. Let's assume we have created a custom label of name category with a value "A Custom Label Value"


Visualforce page

<apex:page controller="custlabelcontroller">
   <apex:form >
     <apex:pageblock >
       Value stored in custom label is: {!customValue}
     </apex:pageblock>
   </apex:form>
</apex:page>



Controller

Public class custlabelcontroller{
Public string customValue{get;set;}
  Public custlabelcontroller(){
   customValue = Label.category;
  }
}






How to add the new line in Salesforce Custom Label

Create a new Custom Label using below path:

Under Setup-> Build -> Create -> Custom Labels -> Click New and create a new custom Label called ‘Test With Line Breaks’




Add the Value like:
Thanks & Regards, <br/>
sfdcsrini.blogspot.com Team <br/>
Testing by sfdcsrini.blogspot.com <br/>

and Save it.


How to Use the Custom Label in Visualforce Page:
<apex:outputText value=”{!$Label.Test_With_Line_Breaks}” escape=”false” /> make sure escape=”false”



No comments:

Post a Comment