• Sumati Patil
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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