1

What's wrong with this Apex/Visualforce code that it returns nothing in my HTML email template? I've tried everything.

Controller:

public class EmailController {
    public Id leadId { get; set; }

    public Lead getLead() {
        Lead lead;
        lead = [SELECT Loan_Cost_Net__c FROM Lead WHERE Id = :leadId];
        return lead;
    }
}

Component:

<apex:component controller="EmailController" access="global">
    <apex:attribute name="lId" type="Id" description="Lead ID" assignTo="{!leadId}"/>

    <apex:repeat value="{!lead.Loan_Cost_Net__c}" var="l">
        {!l}
        <apex:outputPanel rendered="{!IF(l >= 0, TRUE, FALSE)}">
            <apex:outputText value="{0, number, $###,###,###}">
            <apex:param value="{!l}"/>
        </apex:outputText>
    </apex:outputPanel>

    <apex:outputPanel rendered="{!IF(l < 0, TRUE, FALSE)}">
        <apex:outputText value="({0, number, - $###,###,###})">
            <apex:param value="{!l * -1}"/>
        </apex:outputText>
    </apex:outputPanel>
</apex:repeat>

Visualforce:

<h2>Loan Cost for {!lead.Name}</h2>
<c:SSPEmailFormattedCosts lId="{!lead.Id}" />

Things like {!lead.Name} work outside the component, just the component itself returns nothing. How do I even diagnose this?

2 Answers 2

1

The reason for this is in your repeat

<apex:repeat value="{!lead.Loan_Cost_Net__c}" var="l">

the value= needs to be a list, but your controller getLead() is returning only one Lead and the value= is a reference to a single field on a single lead.

I don't see why you are using repeat at all given that the controller only handles a single Lead and doesn't query any children records

Since the title of your post is conditional rendering of stuff, let's try the following:

  1. Eliminate the apex:repeat
  2. Use this VF in place of your outputPanels

    <apex:outputPanel rendered="{!lead.Loan_Cost_Net__c >= 0}">
        <apex:outputText value="{0, number, $###,###,###}">
         <apex:param value="{!lead.Loan_Cost_Net__c}"/>
        </apex:outputText>
    </apex:outputPanel>
    
    <apex:outputPanel rendered="{!lead.Loan_Cost_Net__c < 0}">
      <apex:outputText value="({0, number, - $###,###,###})">
        <apex:param value="{!lead.Loan_Cost_Net__c * -1}"/>
      </apex:outputText>
    </apex:outputPanel>
    

Also, and I'd have to check, I thought that the Java output formatting rules will handle the negative sign automatically if value is negative. You should check on that - that would eliminate the need for conditional rendering altogether

9
  • Short answer: I don't know what option is most suitable instead of repeat. What should it be?
    – t56k
    May 5, 2016 at 2:28
  • @CD-RUM - updated
    – cropredy
    May 5, 2016 at 2:35
  • Your solution makes total sense but I still get no output at all. Do I need to convert the field from currency to a decimal or something? This is tough because the email preview doesn't generate logs either, so it's like the component isn't even loaded in the preview.
    – t56k
    May 5, 2016 at 2:42
  • (1) you do not need to do any conversion, currency fields in apex are decimal ; (2) the reason you may not be seeing anything in Email Template Preview is you have to click the Test and Send button and supply a value for the related to / recipient fields that is a valid Lead Id with a loan Cost Net. The email template preview will render components
    – cropredy
    May 5, 2016 at 2:45
  • Re (2): yeah, that's done. All the other fields in the email outside the component are populated. Even the same field outside the component is populated. Still nothing.
    – t56k
    May 5, 2016 at 2:46
0

So, while @cropredy's answer definitely is more well informed and useful, the actual answer to my question is: you have to select a Visualforce email template type.

Duh.

You must log in to answer this question.

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