+ Start a Discussion
ABC XYZ 39ABC XYZ 39 

How to use Custom Labels in Trigger

Hi, I need to add a list of users to a Cusom Label. I then need to reference this label to allow only users in Custom Label the ability to write data into a custom object using a trigger . I'm new to Apex and Salesforce. Please assist. Thanks.
Best Answer chosen by ABC XYZ 39
Harpreet On CloudHarpreet On Cloud
Account[] accList = [SELECT Id, LastModifiedBy FROM Account LIMIT 1];
if (accList != null  &&  accList.size() > 0) {
  Account acc = accList[0];
  if (System.Label.YOUR_CUSTOM_LABEL_NAME == acc.LastModifiedBy) {
    // Do whatever you want to do here.
  }
}
Harpreet
OSI Consulting

All Answers

Matthew CokeMatthew Coke
labels can be referenced as $Label.(whatever the label name is)
namannaman
Hi,

You can use custom label like below:

string customLabel = label.<your custom label name>;
system.debug('=============label '+customLabel );
ABC XYZ 39ABC XYZ 39
Thank you Matthew and Naman. Is there a way I could compare the "lastmodifiedby" field for a record  with the values in Custom Label. Is there a query I could use? I need to use Apex.
Pankaj_GanwaniPankaj_Ganwani
Hi,

Are you concatenating the User names in comma separated fashion in custom label? If so, Just split them in code and put them in a set. Query on User object based on the last modified by Id to fetch the name. Use set.contains() to find the occurence of User name in set.
Harpreet On CloudHarpreet On Cloud
Account[] accList = [SELECT Id, LastModifiedBy FROM Account LIMIT 1];
if (accList != null  &&  accList.size() > 0) {
  Account acc = accList[0];
  if (System.Label.YOUR_CUSTOM_LABEL_NAME == acc.LastModifiedBy) {
    // Do whatever you want to do here.
  }
}
Harpreet
OSI Consulting
This was selected as the best answer