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
Stefano AmentaStefano Amenta 

If statement in Visualforce

Hi,
I have a simple question but I'm struggling with it as I'm still a beginner in Visualforce.

I created a button called "Print Contract" inside a page that belongs to the custom object Visa__c, and a visualforce page that renders to PDF a contract using html and css.

Here's a piece of code from the Visualforce page as example:
 
<div class="allContainer">
<div class="headerContainer">
    <div class="headerTitle"><label class="labelTitle">Job Contract<br>聘用合同</br></label></div>
    <div class="teacherContainer">    
        <table>
            <tr>
                <th style="width:50%;">1. Personal Info</th>
            </tr>
            <tr>
                <td><label class="labelDetail">Passport Number: </label>
                <apex:outputText styleClass="teacherDetail" value=" {!Visa__c.PassportNumber}"/></td>
            </tr>

However, if the field {!Visa__c.CityName} = Shanghai, when the users click on the Print Contract button I want them to see a different text in the contract, for example, something like this (where Passport is replaced by ID Card).
 
<div class="allContainer">
<div class="headerContainer">
    <div class="headerTitle"><label class="labelTitle">Job Contract<br>聘用合同</br></label></div>
    <div class="teacherContainer">    
        <table>
            <tr>
                <th style="width:50%;">1. Personal Info</th>
            </tr>
            <tr>
                <td><label class="labelDetail">ID Card: </label>
                <apex:outputText styleClass="teacherDetail" value=" {!Visa__c.IDCard}"/></td>
            </tr>

How can I do this by using a IF statement in the Visualforce page? 

Thanks.
sumithasumitha
Hi,

Try like this

<apex:outputlabel value="{!IF(m.Name='sai' ,true,false) }">
{
true condition here
}
</apex:outputlabel>

Thanks,
Sumitha P
 
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi

Actually you can use RENDERED attribute for restricting the display based on other values. You can use this as follows:

 <apex:outputText styleClass="teacherDetail" value=" {!Visa__c.IDCard}" rendered="{!if(Visa__c.Name=='xyz',true,false}"/>

Hope this will be useful.

Thanks
Bhargavi.
Stefano AmentaStefano Amenta
Thanks Bhargavi, I'll try.

If I have multiple values to check for Visa__c.Name, how can I add them?
 
Bhargavi TunuguntlaBhargavi Tunuguntla
You can use AND,OR in that attribute as:  rendered="{!if(or(Visa__c.Name='xyz',Visa__c.Name='yyyy'),true,false}"

Thanks.
Stefano AmentaStefano Amenta
Understood.

How could I hide or show some piece of text where I don't have an outputText value?

For example, how could I hide these titles if Visa__c.Name = Shanghai:
 
<br><tr>
                <th style="width:50%;">5. Contract Dates</th>
                <th style="width:50%; padding-left: 20px">5. 合同日期</th>
            </tr></br>

 
Bhargavi TunuguntlaBhargavi Tunuguntla
Then you can either use <apex:pageblocktable> which even has rendered attribute .If not so you can display the content in <apex:outputpanel> which also has rendered attribute.

In the <apex:pageblocktable> we have<apex:column> even in which you can use using rendered.

Thanks.
sumithasumitha
Hi,

<apex:page standardcontroller="Visa_c" extensions="sample">
<apex:repeat value="{!query}" var="tst">
<apex:outputText styleClass="teacherDetail" value=" {!tst.IDCard}"/></td>
<apex:outputlabel rendered="{!hide}">
<br><tr> <th style="width:50%;">5. Contract Dates</th>
<th style="width:50%; padding-left: 20px">5. 合同日期</th> </tr></br>
</apex:outputlabel>
</apex:repeat>
</apex:page>
apex class:
public class sample
{
public boolean hide{get;set;}
public List<Visa__c> query{get;set;}
{
pulic sample()
{
hide= false; // true means it will display
  query=[select IDCard,(call all fields API name here) from Visa__c where Name = Shanghai];
}
}

Hope it helps,

Thanks,
Sumitha P
 
Stefano AmentaStefano Amenta
Hi Sumitha

Does your code hide only the fields or also the text like "5. Contract Dates" between the <th> tag?
sumithasumitha
Hi Stefano,

It hides all the text/inputfields given inside the
<apex:outputlabel rendered="{!hide}">
//hide all the fields given inside.
</apex:outputlabel>

Thanks
Sumitha P
Stefano AmentaStefano Amenta
Hi Sumitha
thanks again.

A few more questions:

for the class, it's a separate file, right?

I tried to create it but I get an error. I'm not familiar with writing Apex classes so perhaps I'm doing something wrong.
User-added image





 
sumithasumitha
Hi  Stefano,
Sry its a  mistake.
Bracles { is added extra, try the below code.

Apex class:
public class sample
{
public boolean hide{get;set;}
public List<Visa__c> query{get;set;}
public sample()
{
hide= false; // true means it will display
  query=[select IDCard,(call all fields API name here) from Visa__c where Name = Shanghai];
}
}

Thanks,
Sumitha P
Stefano AmentaStefano Amenta
Hi Sumitha

now I get this error:

Error: Unknown constructor 'sample.sample(ApexPages.StandardController controller)'

For now, I only want to hide the text "Social Insurance" so I made something like this:
 
<apex:outputlabel rendered="{!hide}">
            <br><tr>
                <th style="width:50%;">6. Social Insurance</th>
                <th style="width:50%; padding-left: 20px;">6. 社会保险</th>
            </tr></br>
            </apex:outputlabel>
            <br><tr>

and your class:
public class sample
{
public boolean hide{get;set;}
public List<Visa__c> query{get;set;}
public sample()
{
hide= false; // true means it will display
}
}

 
sumithasumitha
Hi Stefano,

Try tis one,

Visual force Page:
<apex:page standardController="Visa__c" extensions="sample">
<apex:outputlabel rendered="{!hide}">
<br>
<tr>
<th style="width:50%;">6. Social Insurance</th>
<th style="width:50%; padding-left: 20px;">6. 社会保险</th>
</tr>
</br>
</apex:outputlabel>
<br>
<tr>
</apex:page> 


public class sample
{
public boolean hide{get;set;}
public List<Visa__c> query{get;set;}
public sample(ApexPages.StandardController controller) 
// should add the content here because in visual force page when standardcontroller is given should add this
{
hide= false; // true means it will display
}
}

Hope it Helps you,

Thanks,
Sumitha P
Stefano AmentaStefano Amenta
Hi, now the text is always hidden.

But if I want to hide it only when CityName = 'Chengdu', where can I add the condition?

I tried to add it in the class but it didn't work:
 
public class sample
{
public boolean hide{get;set;}
public List<Visa__c> query{get;set;}
public sample(ApexPages.StandardController controller) 
// should add the content here because in visual force page when standardcontroller is given should add this
{
hide= false; // true means it will display
query=[select CityName from Visa__c where CityName = 'Chengdu'];
}
}

Thanks so much for all your help so far.