Visualforce Email Template with formula field (IF and HYPERLINK) - Answers - Salesforce Trailblazer Community
Ask Search:
Bailey RuddBailey Rudd 

Visualforce Email Template with formula field (IF and HYPERLINK)

Hello, 

Can someone please help me with the correct way to display a hyperlink in my visualforce email template?
Currently I have a custom formula field (TEXT on Order object) that is an IF statement as follows:
 
IF( BEGINS(Tracking__c , "1Z"), 
HYPERLINK("http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums="&Tracking__c, "Track UPS Ground Shipment"),NULL)
But in the final email that gets sent, the hyperlink is written with HTML tags such as:

User-added image
Best Answer chosen by Bailey Rudd
Sakthivel MadeshSakthivel Madesh
Hi Bailey Rudd,
seems begins conditions is not worked when your track number contain more than two words (for ex:UPS 1Z1234)

Try with the other approach i.e (formula field creations).
Create a formula field with return type as Text
Use the formula:
IF( BEGINS(AccountNumber, "1Z"), 
HYPERLINK("http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums="&AccountNumber, "Track UPS Ground Shipment"),NULL)
Here:
AccountNumber - Your TrackNumber Custom Field

Visualforce Email Template:
Simply use your formula field like below:
Tracking URL - <apex:outputText value="{!RelatedTo.Tracking_URL__c}" escape="false"/>

make sure to include escape="false" in your apex:outputText.

Please try it and let me know if any concerns.

Thanks & Regards,
​​​​​​​Sakthivel Madesh

 

All Answers

Sakthivel MadeshSakthivel Madesh
Hi Bailey Rudd,

There are two ways achive your requirement, there are
Option 1: Create this entirely with custom formula field and use it the newly created formula field in your Visualforce Email Template.
Formula:
IF( BEGINS(Name , "Sakthi"), 
HYPERLINK("http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums="&Name, "Track UPS Ground Shipment"),NULL)

Screenshot's:
User-added image
Output:
User-added image

Use this HyperLink Formula field in your VF Page Email Template.

Option 2:

2) Build logics in your visualforce Email Template as like below
Refer the YouTube Video for the steps - https://www.youtube.com/watch?v=4sEfAPVgtZU
 
<messaging:emailTemplate subject="VF Email Template" recipientType="Contact" relatedToType="Account">
    <messaging:htmlEmailBody >
    
    <div style="display:{!IF(begins(RelatedTo.Name, "Sakthi"), "block", "none")}">
    Account Name is {!RelatedTo.Name} <br/>
    <a href="http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums=&{!RelatedTo.Name}">{!RelatedTo.Name}</a>
    
    OR <br/>
    
    <apex:outputlink value="http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums=&{!RelatedTo.Name}">{!RelatedTo.Name}</apex:outputlink>
    </div>
    
    <div style="display:{!IF(begins(RelatedTo.Name, "Sakthi"), "none", "block")}">
    Account Name is {!RelatedTo.Name} <br/>
    --- There is no account name called Sakthi ----
    </div>
    
    </messaging:htmlEmailBody>
</messaging:emailTemplate>


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks & Regards,
Sakthivel Madesh​​​​​​​
Amit SinghAmit Singh
++ Sakthivel ,

Give the above workaround a try.
Bailey RuddBailey Rudd
Sakthivel,

Thank you very much for the video, it did help get me past a few issues!
I think I'm missing something small though - can you take a look at what I have and see what it is I might be doing wrong?

I already had the custom formula field so here is that formula:

User-added image
Here is my visualforce email template with your suggestions:


User-added image


However, the email template final email has the link included, even though the IF statment is not true, which 
instead I would like it to be just the 2nd line, without the link (since it doesn't start with "1Z") 

User-added image

Does that make sense?
Bailey RuddBailey Rudd
In case you couldn't read the visualforce screenshot here is the code I highlighted:


           
<b>Tracking Number:</b> <div style="{!IF(begins(RelatedTo.Tracking__c, "1Z"),"none","block")}">
          <a href="http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums={!RelatedTo.Tracking__c}">{!RelatedTo.Tracking__c}</a></div>
        
          <div style="{!IF(begins(RelatedTo.Tracking__c, "1Z"),"block","none")}">
          {!RelatedTo.Tracking__c}</div>

 
Sakthivel MadeshSakthivel Madesh
Hi Bailey Rudd,

Try with below code:

<b>Tracking Number to START WITH 1Z:</b> 
<div style="{!IF(begins(RelatedTo.Tracking__c, "1Z"),"block","none")}">

<a href="http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums={!RelatedTo.Tracking__c}">{!RelatedTo.Tracking__c}</a>

OR

<apex:outputlink value="http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums=&{!RelatedTo.Tracking__c}">{!RelatedTo.Tracking__c}</apex:outputlink>

OR  (From Your Formula Field)

<apex:outputText value="{!RelatedTo.Tracking_URL__c}" escape="false"/>

</div>

<b>Tracking Number to NOT START WITH 1Z:</b>

<div style="{!IF(begins(RelatedTo.Tracking__c, "1Z"),"none","block")}">
---Show some message---
</div>


Thanks & Regards,
Sakthivel Madesh
Bailey RuddBailey Rudd
Hi Sakthivel,

I copied and pasted your code, but it's not giving the correct results:

The tracking number does not start with Z, but is still showing links.
User-added image
Sakthivel MadeshSakthivel Madesh
Hi Bailey Rudd,
seems begins conditions is not worked when your track number contain more than two words (for ex:UPS 1Z1234)

Try with the other approach i.e (formula field creations).
Create a formula field with return type as Text
Use the formula:
IF( BEGINS(AccountNumber, "1Z"), 
HYPERLINK("http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums="&AccountNumber, "Track UPS Ground Shipment"),NULL)
Here:
AccountNumber - Your TrackNumber Custom Field

Visualforce Email Template:
Simply use your formula field like below:
Tracking URL - <apex:outputText value="{!RelatedTo.Tracking_URL__c}" escape="false"/>

make sure to include escape="false" in your apex:outputText.

Please try it and let me know if any concerns.

Thanks & Regards,
​​​​​​​Sakthivel Madesh

 
This was selected as the best answer
Bailey RuddBailey Rudd
That worked thank you!