Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have PageBlockTable where i want to put different colors for cells based on some condition

<apex:pageBlockTable value="{!data}" var="rec" width="90%">
        <apex:facet name="header">Values</apex:facet>           
        <div style="text-align:center;font-weight:bold; background-color:{!IF(rec.age ==20,'red', 'blue')}">
            <apex:outputText value="{!rec.age}"/>

The condition i have to check some thing like

else if(age>20 && age<40)
else if(age>40 && age<55)

how can i achieve this.?

Please help

share|improve this question
up vote 7 down vote accepted

The CASE function handles this better than the IF function:

background-color:{!CASE(rec.age <= 20, 'red', rec.age > 20 && rec.age <= 40, 'green', rec.age > 40 && rec.age <= 55, 'blue', 'pink')}

Take care to cover all the possible age values: your pseudo code mapped 40 to pink.

PS Ignore this answer - as crop1645 points out this is not how CASE works. I think the OP must have got their answer from the comment thread so I won't delete this as that would also delete the comments.

share|improve this answer
thanks for reply.I will check this – NullReferenceException Apr 24 '14 at 18:40
@NullReferenceException But, when possible, calculate the value in the controller and express it as a variable. Expressions take up "more space" than simple variables, so if you have many rows of data, you could reach your view state size limit sooner. – sfdcfox Apr 25 '14 at 2:57
@sfdcfox Yes i have many rows.can you please tell me how to do it. – NullReferenceException Apr 25 '14 at 3:21
In the wrapper class: public string getColor() { if(age == 20) return 'red'; if(age < 40) return 'green'; if(age < 55) return 'blue'; return 'pink'; }; in the page: background-color: {!rec.color} – sfdcfox Apr 25 '14 at 3:26
@sfdcfox do i need to write seperate class for the same.?I have 3 columns Age , Age1, Age2 now i am writing style for each column for these 3.It would be great if it is possible to return the colors for 3 column.How can i create wrpper class for the perticular list and send the cell values to it. – NullReferenceException Apr 25 '14 at 3:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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