You need to sign in to do that
Don't have an account?
Roshan Tamrakar
The error I get is: Error: Syntax error. Missing ')'
<apex:column value="{!IF(CONTAINS({!anystring}, 'Anything'),True, False)}"/>
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)
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:
'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
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?
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
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.
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
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
and now the visualforce modified to show adultness
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
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
in visual force templates if u want to use if condition
then the correct syntax is
{!IF(condition,'YES','NO')}
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!!
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>