+ Start a Discussion
Dave BerenatoDave Berenato 

Style Apex PageBlockTable using If Statement

I have an Apex Class that creates a Public List of Tasks
public class AgentTasks {
    
    List<Task> tasks;

    public List<Task> getTasks() {
        if(tasks == null)
            tasks = [SELECT subject, status, owner.name FROM Task LIMIT 10];
        return tasks;
    }
       public PageReference Save()
    {
        update tasks ;
        return new PageReference('/apex/TaskTest');
    } 
}
And I have a Visualforce page that uses an ApexPageBlock Table:
 
<apex:page controller="AgentTasks" id="thePage" standardStylesheets="false">
	<style>
        .StatusClass {
             background-color: {!IF(CONTAINS(tasks.Status,"Not Started"), "red","")};
        }
    </style>
    
    <apex:form>
    <apex:pageblock>
    <apex:pageBlockTable value="{!tasks}" var="task" id="theTable" rowClasses="odd,even" styleClass="tableClass">
        <apex:column headerValue="Status" headerClass="Status" styleClass="StatusClass">
            <apex:outputField value="{!task.Status}" id="Status1"><apex:inlineEditSupport showOnEdit="saveButton, cancelButton" 
            hideOnEdit="editButton" event="ondblclick" changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/></apex:outputField>
        </apex:column>

        <apex:column>
            <apex:facet name="header">Subject</apex:facet>
            <apex:outputText value="{!task.subject}"/>
        </apex:column>

    </apex:pageBlockTable>
        <apex:pageBlockButtons >
            <apex:commandButton action="{!Save}" value="Save"/>
      </apex:pageBlockButtons>
    </apex:pageblock>
    </apex:form>
</apex:page>

If I replace the background-color style tag with just "red" I'm able to save and display a visualforce page where the output values for Status are red. But if I write that line of code :
 
.StatusClass {
             background-color: {!IF(CONTAINS(tasks.Status,"Not Started"), "red","")};
        }

My error message is: Unknown property 'VisualforceArrayList.Status.' How can I fix this so that I can reference it in the style tag?
Best Answer chosen by Dave Berenato
Varun SinghVarun Singh
Hi Dave

Here error message is: Unknown property 'VisualforceArrayList.Status beause you are not using list properties 
this is  wrong  to access a list  values you should  be iterate the values so you are getting error

List Methods
The following are methods for List. All are instance methods.
add(listElement)
Adds an element to the end of the list.
add(index, listElement)
Inserts an element into the list at the specified index position.
addAll(fromList)
Adds all of the elements in the specified list to the list that calls the method. Both lists must be of the same type.
addAll(fromSet)
Add all of the elements in specified set to the list that calls the method. The set and the list must be of the same type.
clear()
Removes all elements from a list, consequently setting the list's length to zero.
clone()
Makes a duplicate copy of a list.
deepClone(preserveId, preserveReadonlyTimestamps, preserveAutonumber)
Makes a duplicate copy of a list of sObject records, including the sObject records themselves.
equals(list2)
Compares this list with the specified list and returns true if both lists are equal; otherwise, returns false.
get(index)
Returns the list element stored at the specified index.
getSObjectType()
Returns the token of the sObject type that makes up a list of sObjects.
hashCode()
Returns the hashcode corresponding to this list and its contents.
isEmpty()
Returns true if the list has zero elements.
iterator()
Returns an instance of an iterator for this list.
remove(index)
Removes the list element stored at the specified index, returning the element that was removed.
set(index, listElement)
Sets the specified value for the element at the given index.
size()
Returns the number of elements in the list.
sort()
Sorts the items in the list in ascending order.

You should try in this way
<apex:page controller="AgentTasks" id="thePage" standardStylesheets="false">

    
    <apex:form>
    <apex:pageblock>
    <apex:pageBlockTable value="{!tasks}" var="task" id="theTable" rowClasses="odd,even" styleClass="tableClass">
        <apex:column headerValue="Status" headerClass="Status"  style="background-color: {!IF(CONTAINS(task.Status,"Not Started"), "red","")}">
            <apex:outputField value="{!task.Status}" id="Status1"><apex:inlineEditSupport showOnEdit="saveButton, cancelButton" 
            hideOnEdit="editButton" event="ondblclick" changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/></apex:outputField>
        </apex:column>

        <apex:column>
            <apex:facet name="header">Subject</apex:facet>
            <apex:outputText value="{!task.subject}"/>
        </apex:column>

    </apex:pageBlockTable>
        <apex:pageBlockButtons >
            <apex:commandButton action="{!Save}" value="Save"/>
      </apex:pageBlockButtons>
    </apex:pageblock>
    </apex:form>
</apex:page>

If  information is  informative please select my answer as best.

All Answers

Varun SinghVarun Singh
Hi Dave

Here error message is: Unknown property 'VisualforceArrayList.Status beause you are not using list properties 
this is  wrong  to access a list  values you should  be iterate the values so you are getting error

List Methods
The following are methods for List. All are instance methods.
add(listElement)
Adds an element to the end of the list.
add(index, listElement)
Inserts an element into the list at the specified index position.
addAll(fromList)
Adds all of the elements in the specified list to the list that calls the method. Both lists must be of the same type.
addAll(fromSet)
Add all of the elements in specified set to the list that calls the method. The set and the list must be of the same type.
clear()
Removes all elements from a list, consequently setting the list's length to zero.
clone()
Makes a duplicate copy of a list.
deepClone(preserveId, preserveReadonlyTimestamps, preserveAutonumber)
Makes a duplicate copy of a list of sObject records, including the sObject records themselves.
equals(list2)
Compares this list with the specified list and returns true if both lists are equal; otherwise, returns false.
get(index)
Returns the list element stored at the specified index.
getSObjectType()
Returns the token of the sObject type that makes up a list of sObjects.
hashCode()
Returns the hashcode corresponding to this list and its contents.
isEmpty()
Returns true if the list has zero elements.
iterator()
Returns an instance of an iterator for this list.
remove(index)
Removes the list element stored at the specified index, returning the element that was removed.
set(index, listElement)
Sets the specified value for the element at the given index.
size()
Returns the number of elements in the list.
sort()
Sorts the items in the list in ascending order.

You should try in this way
<apex:page controller="AgentTasks" id="thePage" standardStylesheets="false">

    
    <apex:form>
    <apex:pageblock>
    <apex:pageBlockTable value="{!tasks}" var="task" id="theTable" rowClasses="odd,even" styleClass="tableClass">
        <apex:column headerValue="Status" headerClass="Status"  style="background-color: {!IF(CONTAINS(task.Status,"Not Started"), "red","")}">
            <apex:outputField value="{!task.Status}" id="Status1"><apex:inlineEditSupport showOnEdit="saveButton, cancelButton" 
            hideOnEdit="editButton" event="ondblclick" changedStyleClass="myBoldClass" resetFunction="resetInlineEdit"/></apex:outputField>
        </apex:column>

        <apex:column>
            <apex:facet name="header">Subject</apex:facet>
            <apex:outputText value="{!task.subject}"/>
        </apex:column>

    </apex:pageBlockTable>
        <apex:pageBlockButtons >
            <apex:commandButton action="{!Save}" value="Save"/>
      </apex:pageBlockButtons>
    </apex:pageblock>
    </apex:form>
</apex:page>

If  information is  informative please select my answer as best.
This was selected as the best answer
Dave BerenatoDave Berenato
Worked perfectly thank you!