Search code examples
htmlsalesforceapexvisualforceemail-templates

Bold Text in Email Template based on IF condition in Salesforce


I need to bold a Text based on an IF statement in Salesforce Visualforce Email Template. Could someone please help me. In below apex html tags I need to bold Required Text when If Condition gets True.

<apex:outputText value="{!IF(AND(NOT(ISBLANK(relatedTo.Quote__r.Margin__c)),NOT(ISBLANK(relatedTo.Quote__r.Threshold__c)),relatedTo.Quote__r.Margin__c<relatedTo.Quote__r.Threshold__c), 
                            "Required", "Not Required" )}" />

Solution

  • This looks bit nasty (duplicated tags) but should be pretty reliable. You could put the condition straight in rendered attributes but if you ever need to change it you could forget and do it in just 1 place, that'd be "fun" bug.

    <apex:variable var="condition" value="{!AND(NOT(ISBLANK(relatedTo.Quote__r.Margin__c)),NOT(ISBLANK(relatedTo.Quote__r.Threshold__c)),relatedTo.Quote__r.Margin__c<relatedTo.Quote__r.Threshold__c)}" />
    
    <apex:outputPanel layout="inline" rendered="{!condition}">
        <b>Required</b>
    </apex:outputPanel>
    <apex:outputPanel layout="inline" rendered="{!!condition}">
        Not Required<!-- you could also put it in `value`-->
    </apex:outputPanel>
    

    If you don't want repeated code you could try something like

    <apex:variable var="condition" value="{!AND(NOT(ISBLANK(relatedTo.Quote__r.Margin__c)),NOT(ISBLANK(relatedTo.Quote__r.Threshold__c)),relatedTo.Quote__r.Margin__c<relatedTo.Quote__r.Threshold__c)}" />
    
    <apex:outputText value="{!IF(condition, 'Required', 'Not Required')}" style="{!IF(condition, 'font-weight:bold, '')}" />
    

    You should test the 2nd versions, what if email client decides to ignore styles. Although inline styles like that should be fine, "just" CSS in <style> tag tends to be unreliable...