+ Start a Discussion
pujapuja 

Dynamic Custom Label

How we'll use Custom label in apex code in which we'll  take the label name dynamically???

Best Answer chosen by Admin (Salesforce Developers) 
Alok_NagarroAlok_Nagarro

Hi Puja,

 

You cant do that in apex, however it can be done in visualforce page using global variable that you can provide the custom label name at runtime.

The following example illustrate this :

 

//-------------------- Controller-----------------------

public class MyController

{

public String mylabel{get; set;}

 

public MyController()

{

 mylabel='Test_label';   // provide custom label name as you have, in my case it is Test_Label

}

}

 

//----------------VF Page ------------------------

 

<apex:page Controller="MyController">

<apex:outputText> {!$Label[mylabel]} </apex:outputText>

</apex:page>

 

Hope it would help u.

All Answers

Alok_NagarroAlok_Nagarro

Hi,

 

Custom labels are not standard sObjects. You cannot create a new instance of a custom label. You can only access the value of a custom label using system.label.label_name in Apex script. 

 

for more about custom Label go through the below link :

 

https://ap1.salesforce.com/help/doc/user_ed.jsp?section=help&target=cl_about.htm&loc=help&hash=topic-title

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

 

=== Vf page==============
<apex:page controller="clscustomlabelvalue" >

<apex:outputText value="{!test1}"></apex:outputText>
</apex:page>

== Apex Controller===========
public class clscustomlabelvalue
{
public string test1{get;set;}
public clscustomlabelvalue ()
{
test1=Label.test;
}
}

 

 

You can access the custom label values dynamically by using the below statement.

 

String test = Label.<custom label name>;

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

 

pujapuja

Hi

    I don't want to directly pass the custom label name  like :  "Label.<Custom Label Name>"....I want to use some variable for this like  :

                            String Str = "Custom_label _Name";

                            String value = System.Label.str;

 

 

Alok_NagarroAlok_Nagarro

Hi Puja,

 

You cant do that in apex, however it can be done in visualforce page using global variable that you can provide the custom label name at runtime.

The following example illustrate this :

 

//-------------------- Controller-----------------------

public class MyController

{

public String mylabel{get; set;}

 

public MyController()

{

 mylabel='Test_label';   // provide custom label name as you have, in my case it is Test_Label

}

}

 

//----------------VF Page ------------------------

 

<apex:page Controller="MyController">

<apex:outputText> {!$Label[mylabel]} </apex:outputText>

</apex:page>

 

Hope it would help u.

This was selected as the best answer
pujapuja

Hi Alok

                Thanks For Help .it's working for me .

Matthieu RemyMatthieu Remy

Hi Alok,

 

Great post! Your solution saved me. Great work figuring out that those square brackets would do the job :-)

 

Matt

Varun RathiVarun Rathi
Following is a code snippet of a work-around. But it has a limitation of working Only in a constructor of a VF page controller class.

string labelName = 'DemoLabel';
Component.Apex.OutputText output;
output = new Component.Apex.OutputText();
output.expressions.value = '{!$Label.' + labelName + '}';
string labelValue = string.valueOf(output.value);

Hope this helps...
Sharad MittalSharad Mittal
Hi,

Can we do it in a formula or validation rule ?

Sharad
Joe Markey 6Joe Markey 6
From a Lightning component perspective it's:
{!$Label.c.My_Custom_Label}

From a Lightning component JavaScript perspective it's:
$A.get("$Label.c.My_Custom_Label");

From a Visualforce page perspective it's:
{!$Label.My_Custom_Label}

From an Apex class perspestive it':
Label.My_Custom_Label