Search code examples
salesforcesalesforce-lightning

Write picklist key in email template instead of display value


I'm need help how to write a Leads country code in an outbound email in Salesforce. I use this email for automation of an external system.

No matter how I try, the full country name is written both as the code and the name.

What I've tried:

I have created a workflow in Process Builder that sends an email when new Lead records are created in Salesforce. The email template looks like this:

companyName: {!Lead.Company}
countryName: {!Lead.Country}
countryCode: {!Lead.CountryCode}

<more code here removed for brewity>

(The email template is referred to through Workflow Actions ->Email Alerts, but that shouldn't be relevant.)

Salesforce is configured to use the default Country picklist for countries. It works great to create new leads in Salesforce through Web-to-Lead web form with country_code defined by:

<select  id="country_code" name="country_code">
  <option value="">--None--</option>
  <option value="CH">Switzerland</option>
</select>

With the above configuration, I can automatically create a new Lead from a Web-to-Lead form. In Salesforce the Lead has the country Switzerland as expected. But the email that is automatically generated by Salesforce writes the country name twice:

companyName: Test company
countryName: Switzerland
countryCode: Switzerland

What I expect:

companyName: Test company
countryName: Switzerland
countryCode: CH

Solution

  • The normal email templates are too simple for what you need. A lot in SF is designed to be user friendly, normally you see picklist labels, never actual values saved in DB. If you have translation workbench enabled you'd see the labels in French/German/what have you.

    After enabling country & state picklists normal text fields disappeared from UI when you make new record. You think you edit the old Country but actually CountryCode is displayed and of course it has value CH but label that's shown is Switzerland. And email will dutifully display a label too.

    I think you need to make a Visualforce email template to achieve this. Something like

    <messaging:emailTemplate subject="62537607" recipientType="User" relatedToType="Lead">
    <messaging:plainTextEmailBody >
    Name: {!relatedTo.Name}
    Company: Name: {!relatedTo.Company}
    Country: {!relatedTo.Country}
    CountryCode: {!relatedTo.CountryCode}
    </messaging:plainTextEmailBody>
    </messaging:emailTemplate>
    

    enter image description here

    With VF email template you get more control, you could even make an Apex controller (well, not for email as whole but for a component and you'd embed the component in the email's body) if you need special behaviour. And by default picklist values will be shown, not labels.

    For example make new picklist entry for Salutation, label "Master blaster of disaster", value "Master". If you add {!relatedTo.Salutation} to the VF template you'll see just Master.