How to use an apex custom label in class?

459    Asked by AvaBlack in Salesforce , Asked on May 12, 2023

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);
Answered by Dipesh Bhardwaj

You can access these apex 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

Your Answer

Interviews

Parent Categories