1

I have a Classic Email Template of Visualforce type. I'm trying to put some dynamics value on the subject attribute base on picklist values, however I'm getting the API Name rather than the label value of the record.

<messaging:emailTemplate

replyTo="[email protected]"    

subject="{!RelatedTo.PickMe__c}"    -> this returns the API name , I want the label   

recipientType="User" relatedToType="Case">

So far , I hoped I could solve it with a CASE function on every API Name, however I have hit the formula length (39xx chars).

Is there some kind of getLabel() function I can apply ? I didn't find one though..

Any idea on how can I solve it ?

2
  • are you amenable to writing a VF component with custom component controller?
    – cropredy
    Commented Jul 24, 2020 at 19:26
  • sure but even though I can't see how, since the component looks like inside emailTemplate
    – AshBringer
    Commented Jul 25, 2020 at 8:50

1 Answer 1

4

To get the label for a picklist value, you can use Apex:

public String getPLVLabel(SObjectField fld, String val)
  Schema.DescribeFieldResult dfr = fld.getDescribe();
  List<Schema.PicklistEntry> plvs = dfr.getPicklistValues();
  
  for (PicklistEntry ple: plvs) {
      if (plv.getValue() == val) {return plv.getLabel();}
  }
  return null;
 }

Because you are trying to get the label in the VF email template's subject, using a custom component and controller is not going to help because subject is an attribute of messaging:emailTemplate and it can only be:

  • a constant
  • a merge field of recipientType
  • a merge field of relatedToType

So, you're going to need a shadow field on Case called PickMe_AsLabel__c that you populate in the before update/before insert Case trigger using the above ccode, invoked like this:

for (Case cs : Trigger.new) {
   cs.PickMe_AsLabel__c = new MyUtil.getPlvLabel(Case.PickMe__c,cs.PickMe__c);
}
2

You must log in to answer this question.

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