Search code examples
htmlsalesforceapexvisualforce

Visualforce: Render only if value is not null throws exception


I'm building a visualforce page where I have a apex:datatable with several columns.

Now I want the column "Discount" only to be shown when its values are not null and >0.

My Code is as following:

<apex:dataTable width="100%" value="{!Opportunity.OpportunityLineItems}" var="oli">
                            <apex:column width="50%" headerClass="tableheaderleft"  styleClass="tablebodyleft">
                                <apex:facet name="header">Bezeichnung</apex:facet>
                                <apex:OutputField value="{!oli.Name}"/>
                             </apex:column>
                            <apex:column width="{!If(oli.Discount!=null,If((oli.Discount>0),"10%","10%"),"10%")}" headerClass="tableheadercenter" footerClass="tablefootercenter" styleClass="tablebodycenter">
                                <apex:facet name="header">Anzahl</apex:facet>
                                <apex:OutputField value="{!oli.Quantity}"/>
                                <apex:facet name="footer"></apex:facet>
                            </apex:column>
                            <apex:column width="20%" headerClass="tableheaderright"  styleClass="tablebodyright">
                                <apex:facet name="header">Einzelbetrag</apex:facet>
                                <apex:OutputField value="{!oli.UnitPrice}"/>
                                <apex:facet name="footer"></apex:facet>
                            </apex:column>
                            
                         
                           <apex:column  rendered="{!If(oli.Discount!=null,(oli.Discount>0),false)}" headerClass="tableheadercenter" footerClass="tablefootercenter" styleClass="tablebodycenter">
                                <apex:facet name="header">Rabatt</apex:facet>
                                <apex:OutputField value="{!oli.Discount}"/>
                                <apex:facet name="footer"></apex:facet>
                            </apex:column>  
                            
                            <apex:column width="20%" headerClass="tableheaderright"  styleClass="tablebodyright">
                                <apex:facet name="header">Gesamtbetrag</apex:facet>
                                <apex:OutputField value="{!oli.TotalPrice}"/>
                                <apex:facet name="footer"></apex:facet>
                            </apex:column>                                                                                            
                        </apex:dataTable>

But I always get the following exception:

javax.faces.FacesException: core.apexpages.exceptions.ApexPagesDeveloperException: Incorrect argumenttype for operator '>'.

Does anybody know why this exception occures? Could it occur because Discount returns a percentage value?

Many Thanks!


Solution

  • You clarified in comments "Only want to show the whole column if at least one row has a value".

    Do you have a Discount-related rollup summary field from Opp Line Item to Opp? You could do something like rendered="{!Opportunity.DiscountLinesCount__c > 0}" or {!Opportunity.TotalDiscount__c > 0}.

    If you'd have apex controller (or extension) for it you could check the lines and set some boolean flag and then render based on that. That' be the official way.

    Unofficial you could loop through line items before your dataTable, abusing the <apex:variable> tag. It's bit of hack but it'd work without any apex. Check my answer to this question: https://salesforce.stackexchange.com/q/193169/799

    Something like this. Didn't try to run it but should give you idea.

    <apex:variable var="hasDiscountLines" value="{!false}" />
    <apex:repeat value="{!Opportunity.OpportunityLineItems}" var="oli">
        <apex:variable var="hasDiscountLines" value="{!hasDiscountLines || oli.Discount != null}" />
    </apex:repeat>
    
    (...)
    <apex:dataTable width="100%" value="{!Opportunity.OpportunityLineItems}" var="oli">
        <apex:column  rendered="{!hasDiscountLines}">
        (...)
    </apex:dataTable>