function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Amanda MalmstromAmanda Malmstrom 

apex:repeat in visualforce email template is working on one object but not another object

Hello, 

I have logic in an email that works perfectly on one custom object, but when repurposing that logic into another custom object's email it does not work.

This is my snipit that does not work:

<apex:repeat var="cb" value="{!relatedTo.Advance__r.CoBrand__c}">
<apex:outputPanel rendered="cb == 'BrandA'">
The BrandA Company
</apex:outputPanel>
<apex:outputPanel rendered="cb != 'BrandA'">
The BrandB Company
</apex:outputPanel>
</apex:repeat>


I checked and {!relatedTo.Advance__r.CoBrand__c} does populate on its own within the email body, but the apex statement does not render any text. I had to go with the == and != approach because CoBrand__c can have many values but the only time the statement needs to be different is for BrandA.

Is there something wrong with the syntax? 

 
TechingCrewMattTechingCrewMatt
Hello, Amanda. The repeat tag is expecting a list of objects. It's unclear what you're hoping will be rendered. What type of field is CoBrand__c? It looks like it's either a text or a picklist field. How many CoBrand__c's will there be for each related Advance__c record?

Thanks, 
Matt
Amanda MalmstromAmanda Malmstrom
Thank you for looking into it, I realized I was missing the IF statement to make it work. There will only be one cobrand per advance, its just possible that it can be a few different values. 

Correct format:

<apex:repeat var="cb" value="{!relatedTo.Advance__r.CoBrand__c}">
<apex:outputText rendered="{! IF(cb == 'BrandA', true, false)}">
The BrandA Company
</apex:outputText>
<apex:outputText rendered="{! IF(cb != 'BrandA', true, false)}">
the BrandB Company
</apex:outputText>
</apex:repeat>





 
TechingCrewMattTechingCrewMatt
You don't need an IF statement to return true or false. The conidtion itself already returns true or false. If you are not iterating over a collection of objects, the <apex:repeat> tag is not the correct solution. The most elegant solution is below:
<apex:outputText value="{0} Company">
    <apex:param value="relatedTo.Advance__r.CoBrand__c">
</apex:outputText>

 
Amanda MalmstromAmanda Malmstrom
The text rendered doesnt match the field's value specifically. Also it makes total sense that I don't need the IF statement logically, but when the IF statement is not there it renders no text in the email, with the IF statement it renders whatever statement I put under the output text if CoBrand is a certain value. Another example being if the value = ComanyA output 'The sky is blue' but if the value = CompanyB output 'I want a pizza'.  If there is a simpler solution I'm all for it, that was just how I got it to work in the past.