7

I have the below If statements using some hard coded strings and I want to make them use as custom labels. I have created custom labels with the same String names as given below. So how can I make these statements to use the Custom Labels I created ?

if(monitoringFrequency == 'daily'){
            return dateValue.addDays(1);
        }else if(monitoringFrequency == 'weekly'){
            return dateValue.addDays(7);
        }else if(monitoringFrequency == 'monthly'){
            return dateValue.addMonths(1);

2 Answers 2

11

You can access these custom labels like this:

if(monitoringFrequency == System.Label.daily){
    return dateValue.addDays(1);
}else if(monitoringFrequency == System.Label.weekly){
    return dateValue.addDays(7);
}else if(monitoringFrequency == System.Label.monthly){
    return dateValue.addMonths(1);
}

Do make sure that the names of the custom labels are correct.

For more information, see: https://help.salesforce.com/HTViewHelpDoc?id=cl_about.htm&language=en_US

2
  • 8
    If you want your code to be more terse you can omit the System. and just use Label. with no issues. Commented Jan 22, 2015 at 11:29
  • +1 for using System.Label.[...] consistently. Helps to avoid debugging when also using a variable called label or another Apex Class called Label Commented Mar 7, 2018 at 17:06
4

We can put label in salesforce these ways:

Visualforce Page: $Label.Label_API_Name

Lightning Component Page {!$Label.c.Label_API_Name}

Apex Class String getLabelName = Label.Label_API_Name;

or directly System.Label.Label_API_Name

hope this helps you.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .