+ Start a Discussion
Roger PavelleRoger Pavelle 

Can you put a conditional inside an apex tag?

I'm importing a database integer value into an apex page. 
In one place, I would like to replace the value with text (if value = 1 text=Bad, else if value = 2 text = blah, etc.).
In a different place, I'd like to color code backgrounds based on the value (If value =1 background = red, else if value = 2 background = yellow, etc.)

I know I can figure out a workaround for the first example, but since the second one needs something programmatic.  Is there a way to put an if statement (or case statement or other conditional statement) in the apex tag?
Best Answer chosen by Roger Pavelle
Ricky ThedaRicky Theda
Hi Roger,

You can try this:
1. First create a css class like this:
<style>
.redbg{
background-color: red;
}
</style>
2. And attach the class to apex element with if clause:
<apex:inputText id="field_id" styleClass="{!IF(value == 1,'redbg',IF(vallue == 2,'yellowbg',etc))}"/>

hope this help.

All Answers

William TranWilliam Tran
It depends on how you want to code it but there are a few ways you can go about it, but yes can you work conditional statements in a VF page.

1)                 <apex:repeat value="{!relatedTo.Legal_Requests__r}" var="legalRequest">
                        <tr>
                            <td>
                                {!IF(AND(legalRequest.Request_Type__c == 'Sales', legalRequest.Service_Type__c == 'New Business'), <a href ="https://fico--dev1.cs30.my.salesforce.com/{!legalRequest.Id}">{!legalRequest.Name}</a> , false)}
                            </td>                                                         
                        </tr>
                </apex:repeat>

2) You can also put all the condition statement in your extension or controller class and return what final results.

That are other options also such as scriptings.

Thx
Ricky ThedaRicky Theda
Hi Roger,

You can try this:
1. First create a css class like this:
<style>
.redbg{
background-color: red;
}
</style>
2. And attach the class to apex element with if clause:
<apex:inputText id="field_id" styleClass="{!IF(value == 1,'redbg',IF(vallue == 2,'yellowbg',etc))}"/>

hope this help.
This was selected as the best answer
Roger PavelleRoger Pavelle
Thanks.  That's just what I was looking for.
Roger PavelleRoger Pavelle
The If statement certainly works for a single row.  However, I am not getting it to work for a table.  What I want is for each row to have a background color that is based on the Edibility__c database field. 

Any suggestions?

Here is the IF statement that I am trying to use.  I get an error if I put this into the Search Results block or if I try to attach it to the pageBlockTable tag (the latter because it doesn't recognize the variable name.

            <apex:outputPanel styleClass="{!IF(bol.Edibility__c == 1,'red',
                                                       IF(bol.Edibility__c  == 2,'yellow',
                                                       IF(bol.Edibility__c  == 3,'blue',
                                                       'green')))}">

Here is the code for the table.

            <apex:pageBlock title="Search Results"  id="bolete_list">
                <apex:outputText >{!showResultSize} of {!showRecordSize} records displayed</apex:outputText>
                <br/>
                <apex:pageBlockTable value="{! Boletes }" var="bol" style="width: 100%">
                    <apex:column width="75">
                        <apex:outputLink value="/apex/boleteDetailView?id={!bol.Id}" id="viewLink">
                            View
                        </apex:outputLink>
                    </apex:column>
                   
                    <apex:column width="75">
                        <apex:image id="imageURL" url="{!bol.ImageURL__c}" height="75"/>
                    </apex:column>

                    <apex:column value="{! bol.genus__c }">
                        <apex:facet name="header">
                            <apex:commandLink action="{! sortByGenus }" reRender="bolete_list">Genus
                            </apex:commandLink>
                        </apex:facet>
                    </apex:column>
                   
                    <apex:column value="{! bol.species__c }">
                        <apex:facet name="header">
                            <apex:commandLink action="{! sortBySpecies }" reRender="bolete_list">Species
                            </apex:commandLink>
                        </apex:facet>
                    </apex:column>
                   
                    <apex:column value="{! bol.Common__c }"/>
                </apex:pageBlockTable>
            </apex:pageBlock>
        </apex:pageBlock>
 
Ricky ThedaRicky Theda
Roger,
Can you make sure that your visualforce page have controller? Because when you want to call field from any object you must have a controller embedded on the pages, something like:
<apex:page standardController="Opportunity" extensions="ShowUnitController"  sidebar="false">
 
Roger PavelleRoger Pavelle
I am using a custom object and therefore have built a custom controller.