I would like to check the size of the OpportunityLineItem related list in a Visualforce Email Template like below, where relatedTo is an Opportunity

<apex:outputPanel rendered="{!relatedTo.OpportunityLineItems.size != 0}">

But I get error:

Error: Invalid field 'size' for SObject 'OpportunityLineItem'.

What is the correct way to achieve this?

  • What is relatedTo here? – Raul May 20 '19 at 9:59

You won't be able to get the size directly in visualforce email template, for that you will have to use the repeat tag to get the count and then you can build further logic based on that variable.

<apex:variable var="sizeOfOpportunityLineItem" value="{!0}"/>
<apex:repeat value="{!relatedTo.OpportunityLineItems}">
    <apex:variable var="sizeOfOpportunityLineItem" value="{!sizeOfOpportunityLineItem + 1}"/>
Number of Opportunity Line Items : {!sizeOfOpportunityLineItem}
  • This is not a Visualforce Page its an Visualforce Email Template I don't have a controller – Robs May 20 '19 at 10:15
  • Updated the answer, that should work for you. – Vijay Ganji May 20 '19 at 10:25
  • Thanks Vijay. It works. But I will be honest, I am not keen on the solution, it seem unnecessarily convoluted. – Robs May 20 '19 at 10:35
  • Yup I can understand, but AFAIK there is no other way. @sfdcfox : Can you please see if this can be done in a better and simpler way. – Vijay Ganji May 20 '19 at 10:46

Look like your OpportunityLineItems is an OpportunityLineItem instead of a List.

  • But I am using OpportunityLineItems not OpportunityLineItem – Robs May 20 '19 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.