How do I output 1 field value from a relatedlist based on the condition that there is 1 record with that value. I tried the following but I get an syntax error see:

<apex:repeat var="ex" value="{!Opportunity.OpportunityLineItems}">
IF(ex.Product2=='20ft',<apex:variable var="a" value="{!ex.Product2}" />,'')
<apex:outputText> {!IF({!a}=='20ft',{!a},'')} </apex:outputText>

1 Answer 1


You can display it while iterating itself. Use rendered attribute.

<apex:repeat var="ex" value="{!Opportunity.OpportunityLineItems}">
   <apex:outputText rendered="{!IF(ex.Product2=='20ft',true,false)}">

For finding the last item after full iteration;

<apex:variable var="outputProd2" value=""/>
<apex:repeat var="ex" value="{!Opportunity.OpportunityLineItems}">
   <apex:variable var="outputProd2" value="{!IF(ex.Product2=='20ft',ex.Product2,outputProd2)}"/>
<apex:outputText> {!outputProd2} </apex:outputText>
  • Thanks for your answer. Since this is within the repeat, if there are more records with true I will get several outputs. In my case I only want to show that 1 type of product is used despite of have several records with the same product. for example record(1,2,1,1,1,2,2,3,5,6,7,10,10,10,1,1) IF repeat record.field==1 Output 1,IF repeat record.field==10 Output 10
    – Jenssen
    May 17, 2017 at 8:46
  • @Jenssen I have added a second part to my answer. Check if this helps. I have not compiled the code. Please let me know if you get any compilation error. May 17, 2017 at 9:10
  • thanks for helping but I get the error: Error: Unknown property 'OpportunityStandardController.outputProd2'
    – Jenssen
    May 18, 2017 at 6:31
  • I Got this working the following way Thanks!, If jou edit your answer I will accept the solution: ' <apex:repeat value="{!Opportunity.OpportunityLineItems}" var="ex"> <apex:variable value="" var="outputProd2"/> <apex:variable var="outputProd2" value="{!IF(ex.Product2.Name=='20ft',ex.Product2.Name,outputProd2)}"/> </apex:repeat> <apex:outputText> {!outputProd2} </apex:outputText>'
    – Jenssen
    May 18, 2017 at 7:18
  • I guess setting value as {!null} in the first line was the problem. I have removed it. But in the code in above comment you have added <apex:variable value="" var="outputProd2"/> inside the repeat. This will erase the field value each time. Keep this above the <apex:repeat/> May 18, 2017 at 7:46

You must log in to answer this question.

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