function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Roshan TamrakarRoshan Tamrakar 

If Condition in VisualForce Page

Hello experts,
 
I want to know the way of using condition in visualforce page. I have a custom object 'Person__c' having few fields. Two of them are:
Name, Active(Checkbox), Birthday, Age(Numberic)
 
What I want to do is list values in these fields in visualforce page. While displaying, I want to display:
 'Minor' if Age<16, otherwise 'Adult'
 
I want to be able to do this in any of the following 3 ways:
 
1. By using If clause in apex page
2. By passing 'Age' as a parameter to my extended controller
3. And using case (or similar) in SOQL
 
Below is my code:
 
Code:
<apex:page standardController="Person__c" extensions="PersonExtension">
 <apex:form>
  <apex:pageblock title="My Persons Infos">
   <apex:pageblockList value="{!PersonList}" var="p">
    <apex:column value="{!p.Name}"/>
    <apex:column value="{!p.Active__c}"/>
    <apex:column value="{!p.Birthday__c}"/>
    <apex:column value="{!p.Age__c}"/>
    <apex:column value="{!anystring}"/>
    <apex:column value="{!IF((p.Age__c>16),'Adult','Minor')}"/>
   </apex:pageblockList>
  </apex:pageblock>
 </apex:form>
</apex:page>


public class PersonExtension{

  private final Person__c person;

  public PersonExtension(ApexPages.StandardController stdController){
    this.person= (Person__c)stdController.getRecord();
  }

  public Person__c[] getPersonList(){
    return [Select p.Id, p.Name, p.Age__c, Birthday__c, p.Active__c from Person__c p];
  }

  public String getAnyString(){
    return 'Anything';
  }
}

The error I get is: Error: Syntax error. Missing ')'
 
If I change the if condition to: <apex:column value="{!IF((p.Age__c<16),'Minor','Adult')}"/>
The error will be: Error: Syntax error. Found 'lt'
 
Even a simple clause is not working:

<apex:column value="{!IF(CONTAINS({!anystring}, 'Anything'),True, False)}"/>
 
1. What is wrong in my code? Can I use IF condition in this way?
2. Can I pass a parameter (Age in above case) to my controller and return a string based on parameter supplied to it? If so, how?
3. Can I use case...when clause (or similar like in sql) in my SOQL? If so, how can I do so?
 
Thanks
HalcyonandonHalcyonandon
I'm not an expert here but... proper syntax in the documentation says to use double quotes...

Code:
{!IF((p.Age__c>16),"Adult","Minor")}

{!IF((p.Age__c<16),"Minor","Adult")}

{!IF(CONTAINS({!anystring}, "Anything"),True, False)}

Also you may want to use a <=  or >= operator, depending on what you consider a minor, but that doesnt mean anything really to your issue. 

Let me know if this works for you, I'm curious on the results myself.





Ron HessRon Hess
This appears to be a bug, i have filed a bug report.

In general you should not use logic inside your page, logic should be in the controller or controller extension. The page primarily for rendering data, Visualforce employs the Model View Controller design pattern, and putting logic onto the page is contraindicated.


Here are some options:
at it's simplest, you could create a field on person__c object which is a formula field that shows Adult or Minor and is then simply displayed on the visualforce column.


or use rendered, like this:
big
small

this however, does not save for the reason you reported ( Error: Syntax error. Found 'lt'), i've marked this issue for review by our developers.


You can also wrap person__c with a class that adds a method for getMinorAdult() which returns the string you require, then build a list of this class to return to your page to iterate thru. This will work, and would basically cover your option #3 as it is all done in the Apex Controller extension class.

Message Edited by Ron Hess on 04-28-2008 08:11 AM
Roshan TamrakarRoshan Tamrakar

Thanks Ron,

I agree with you that, logics are best put in controller. But being a new, I don't know how to do. Can you please write a sample code based on my illustration?

The formula way, as I know, one of the solutions. But I believe, creating a field may not be feasible for minor requirements. So how do I pass a parameter to a controller?

I also seek your help on your last paragraph...

"You can also wrap person__c with a class that adds a method for getMinorAdult() which returns the string you require, then build a list of this class to return to your page to iterate thru. This will work, and would basically cover your option #3 as it is all done in the Apex Controller extension class."

I would highly appreciate if you could show me with a little code how to do this.

Thanks

-Roshan
Ron HessRon Hess
Here you go, i switched to contact records, first the Apex Controller

Code:
public class PersonExtension{

  private final Contact person;
  
  public PersonExtension(ApexPages.StandardController stdController){
    this.person= (Contact)stdController.getRecord();
  }
  public class People { 
   contact c;
   public People(Contact cin) { c = cin; }
   public Contact getContact(){ return c;}
   public String getAgeType()  { 
    if (c.Age__c == null ) return 'unknown';
    if (c.age__c < 21) return 'minor';
    return 'adult';
   }
  }
  List<People> peeps = new List<People>{};
  public List<People> getPersonList() { 
    peeps.clear(); 
    for (contact cc: [Select p.Id, p.Name, p.Age__c  from Contact p order by lastname limit 10] ) {
    peeps.add( new People (cc) );
    }
    return peeps;
  }

  public String getAnyString(){
    return 'Anything';
  }
}

 
and now the visualforce modified to show adultness

Code:
<apex:page standardController="Contact" extensions="PersonExtension">
 <apex:form>
  <apex:pageblock title="My Persons Infos">
   <apex:pageblockList value="{!PersonList}" var="p">
    <apex:column value="{!p.Contact.Name}"/>
    <apex:column value="{!p.Contact.Age__c}"/>
     <apex:column value="{!p.ageType}" headerValue="Adultness" />
    <apex:column value="{!anystring}" headerValue="my String" />
   </apex:pageblockList>
  </apex:pageblock>
 </apex:form>
</apex:page>

 

Hope this makes it easy to understand how to put logic into apex code, not on the page.
Roshan TamrakarRoshan Tamrakar
Thanks Ron,

It's a great help to me. By following your suggestion, I am able to create my own logic inside the controller and use number of if clauses that I was looking for.

Thanks again,
-Roshan
Shane_CRShane_CR

Hi All,

How can I change the styleclass css in apex cloumn using the if condition  visualforce page.

My Code:

<apex:column headerValue="Agreement ID" styleclass="grid_head_pad" headerclass="tr_bg3" value="{!NotificationBO.BSAId}" rendered="{!DisplayBSAId}"/>

Regards

Shane

 

GayatriGayatri

in visual force templates if u want to use if condition

then the correct syntax is

{!IF(condition,'YES','NO')}

Swati TaunkSwati Taunk

I want to use IF as follows:

 

{!IF(MOD(i,2) == 0,'</tr>',' ')}

 

There is no error but the above statement considers <tr> as string and displays it instead of considering a HTML tag..

PLZ help!!

mramaprasad.ax1826mramaprasad.ax1826
I have three values for p.Certification_Type__c (cert1, cert2 and cert3).
When cert1 or cert2 are selected from the drop down I want to display p.Name otherwise I want to display p.Certification_Number__c.
When I try the follwoing code I get  Authorization required error. Please help!.

<apex:commandButton value="Search" action="{!doSearch}"
                        rerender="found,msg" />
                    
                    </apex:panelGrid>
                   
                   
                    <apex:pageBlocktable value="{!certlist}" var="p">                 
                    <apex:variable var="flag" value="{!IF(((p.Certification_Type__c = 'cert1') || (p.Certification_Type__c = 'cert2')),true,false)}" />
                    <apex:variable var="flag2" value="false" />
                    <apex:variable var="type1" value="" />
                   
                      
                       <apex:column headerValue="Certification Number"  value="{!p.Certification_Number__c}" />
                      
                       <apex:column headerValue="certificate Name" value="{!p.Name}" />
                       <apex:column value="{!IF((flag),p.Name,p.Certification_Number__c)}"/>                    
                      
                      
                   </apex:pageBlocktable> 
                </apex:outputPanel>
               
            </apex:pageBlockSection>
Sumati PatilSumati Patil
Correct format to use in VF page is : {!IF(CONTAINS(anystring, 'Anything'),True, False)}