3

I have picklist field and I need to get in the trigger both old and new translated values. I can query the new value from the record:

Lead lead = [select id, tolabel(status) from Lead where id = :leadId];

But i still haven't find a way to get the old translated value.

  • you can get the old values in trigger.old, list of the old versions of the sObject records. but this sObject list is only available in update and delete triggers. And you can use trigger.oldMap also. – Safiya PV Apr 2 '17 at 11:57
  • The value in trigger (old or new) is the API name. To get the translated value I need to query it, however query give me only the new translated value. – Liron C Apr 2 '17 at 12:03
5

In Apex, picklist fields on objects are just plain strings, and lack a method to get the label.

You also can't query the previous state of the database.

You will have to use the system describe methods to get the Schema.PicklistEntry values for your field, and use them to associate the api values in the trigger context variables with the labels from the describe call.

// Use the describe information to generate a mapping from api name to label
list<Schema.PicklistEntry> PicklistEntries = Lead.Status.getDescribe().getPicklistValues();
map<String,String> ApiToLabel = new map<String,String>();
for (Schema.PicklistEntry pe : PicklistEntries){
    ApiToLabel.put(pe.getValue(),pe.getLabel());
}

// use the map to get the label from the api values in the trigger context variables
for (Lead l : trigger.old){
    system.debug('api value: ' + l.Status);
    system.debug('label: ' + ApiToLabel.get(l.Status));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.