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
jill.longojill.longo 

Help with Visualforce IF statement

I need to create a table that contains only contact records where a custome field called Escalation Contact is checked. The table should contain the Name of the contact and phone number. I'm having trouble with my IF statement and don't know if i have it placed in the correct location.

<apex:page standardController="Account">
<apex:pageBlock >
    <apex:pageBlockTable value="{! account.contacts}" var="item">
        <apex:column value=" {IF({!item.Escalation_Contact__c},"{!item.name}", "")}"/>
         <apex column value="{! item.phone}"/>  
           
    </apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
Best Answer chosen by jill.longo
Andy BoettcherAndy Boettcher
Ok Jill!  I pseudocoded something together here...I haven't confirmed 100% accuracy in an IDE but you can take this to start and tinker....feel absolutely free to ping me back with questions.

Step 1:  Create a new Apex Class (your controller)
 
public with Sharing class AccountEscalationContact {
    public ApexPages.standardController scMain {get;set;}  
    public AccountEscalationContact(ApexPages.standardController sc) {
        scMain = sc;
    }
    public getlstContacts() {
        return [SELECT Id, Name, Phone FROM Contact
                 WHERE AccountId = :scMain.getId()
                 AND Escalation_Contact__c = true
                 ORDER BY Name ASC LIMIT 1000];
    }
}
 
Step 2:  A quick change to your existing VF page:
 
<apex:page standardController="Account" extensions="AccountEscalationContact" >
  <apex:pageBlock >
    <apex:pageBlockTable value="{!lstContacts}" var="item">
       <apex:column value="{!item.name}"/>
       <apex column value="{!item.phone}"/>  
    </apex:pageBlockTable>
  </apex:pageBlock>                      
</apex:page>
 
Step 3:  Then you need a Unit Test (another Apex Class) to deploy everything to Production:
 
@isTest
private class UnitTest_AccountEscalationContact {
 
  @isTest static void testController() {
     Account acct = new Account(Name='Test Account');
     insert acct;
 
     Contact conFalse = new Contact(AccountId=acct.Id);
     conFalse.Lastname = 'testfalse';
     conFalse.Email = 'me@you.com';
     conFalse.Escalation_Contact__c = false;
     insert conFalse;
 
     Contact conTrue = new Contact(AccountId=acct.Id);
     conTrue.Lastname = 'testtrue';
     conTrue.Email = 'me@you.com';
     conTrue.Escalation_Contact__c = true;
     insert conTrue;
 
     test.startTest();
     AccountEscalationContact clsAEC = new AccountEscalationContact(new ApexPages.standardController(acct));
     List<Contact> lstContacts = clsAEC.getlstContacts();
     System.Assert(lstContacts.size() == 1, 'did not find one escalation contact!');
     test.stopTest();
  }
}


All Answers

Vinit_KumarVinit_Kumar
Try this one,

<apex:page standardController="Account">
<apex:pageBlock >
    <apex:pageBlockTable value="{! account.contacts}" var="item">
        <apex:column value="{!item.name}" rendered = "{IF(!item.Escalation_Contact__c==true),true,false}"/>
         <apex column value="{! item.phone}"/>  
           
    </apex:pageBlockTable>
</apex:pageBlock>
</apex:page>

If this helps,please mark it as best answer to help others :)
Ramu_SFDCRamu_SFDC
If you want to display the filtered data you would need to have a controller where you should be querying for the results you need and pass that as a list to the visualforce component. Follow the guidelines as explained in the below article.

http://www.infallibletechie.com/2013/02/how-to-fetch-data-from-controller-and_6.html

Please mark this as the best answer if this resolved your requirement.
jill.longojill.longo
When i insert this code, it's still showing me all 4 contacts and not just the one that is marked as the escalation contact. I also need to see the contact name. [X]
Vinit_KumarVinit_Kumar
Then,you need to query the records inside your controller like Ramu said and call that on page load or any action method.
Andy BoettcherAndy Boettcher

Ramu_SFDC is correct - to filter the data that appears in a list, you will need to query that data from an APEX controller.  Were you able to figure this out or do you need further assistance?

jill.longojill.longo
I took a look at the code Ramu_SFDC sent in the link and am lost on where to begin. Any further assistance is appreciated. Jill
Andy BoettcherAndy Boettcher
Absolutely Jill!  Is your existing VF page placed within your Account page layout or is it a standalone VF page?
jill.longojill.longo
it's placed within the Account Page as a related list
Andy BoettcherAndy Boettcher
Ok Jill!  I pseudocoded something together here...I haven't confirmed 100% accuracy in an IDE but you can take this to start and tinker....feel absolutely free to ping me back with questions.

Step 1:  Create a new Apex Class (your controller)
 
public with Sharing class AccountEscalationContact {
    public ApexPages.standardController scMain {get;set;}  
    public AccountEscalationContact(ApexPages.standardController sc) {
        scMain = sc;
    }
    public getlstContacts() {
        return [SELECT Id, Name, Phone FROM Contact
                 WHERE AccountId = :scMain.getId()
                 AND Escalation_Contact__c = true
                 ORDER BY Name ASC LIMIT 1000];
    }
}
 
Step 2:  A quick change to your existing VF page:
 
<apex:page standardController="Account" extensions="AccountEscalationContact" >
  <apex:pageBlock >
    <apex:pageBlockTable value="{!lstContacts}" var="item">
       <apex:column value="{!item.name}"/>
       <apex column value="{!item.phone}"/>  
    </apex:pageBlockTable>
  </apex:pageBlock>                      
</apex:page>
 
Step 3:  Then you need a Unit Test (another Apex Class) to deploy everything to Production:
 
@isTest
private class UnitTest_AccountEscalationContact {
 
  @isTest static void testController() {
     Account acct = new Account(Name='Test Account');
     insert acct;
 
     Contact conFalse = new Contact(AccountId=acct.Id);
     conFalse.Lastname = 'testfalse';
     conFalse.Email = 'me@you.com';
     conFalse.Escalation_Contact__c = false;
     insert conFalse;
 
     Contact conTrue = new Contact(AccountId=acct.Id);
     conTrue.Lastname = 'testtrue';
     conTrue.Email = 'me@you.com';
     conTrue.Escalation_Contact__c = true;
     insert conTrue;
 
     test.startTest();
     AccountEscalationContact clsAEC = new AccountEscalationContact(new ApexPages.standardController(acct));
     List<Contact> lstContacts = clsAEC.getlstContacts();
     System.Assert(lstContacts.size() == 1, 'did not find one escalation contact!');
     test.stopTest();
  }
}


This was selected as the best answer
jill.longojill.longo
@andyboettcher I get the follow error message when creating the class "Invalid Constructor Name: getlstContacts at line 6 column 12"
Andy BoettcherAndy Boettcher
Hi Jill! Was out of town, I'll respond tonight. -------- Original message -------- From: Salesforce Developer Community Date:07/11/2014 4:30 PM (GMT-06:00) To: Andy Boettcher Subject: (Salesforce Developers): New reply to a question you're following. jill.longo jill.longo replied to a question you're following at 4:30 PM on 7/11/2014. Your question: “I need to create a table that contains only contact records where a custome field called Escalation Contact is checked. The table should contain the Name of the contact and phone number. I'm having trouble with my IF statement and don't know if i have it placed in the correct location. ” Reply: “@andyboettcher I get the follow error message when creating the class "Invalid Constructor Name: getlstContacts at line 6 column 12" ” Tip! To respond, either reply to this email or click this link: https://developer.salesforce.com/forums/ForumsMain?id=906F0000000Ab03
Vinit_KumarVinit_Kumar
Hey jill,

Change your code from 

public getlstContacts() {
        return [SELECT Id, Name, Phone FROM Contact
                 WHERE AccountId = :scMain.getId()
                 AND Escalation_Contact__c = true
                 ORDER BY Name ASC LIMIT 1000];
    }

to

public List<Contact> getlstContacts() {
        return [SELECT Id, Name, Phone FROM Contact
                 WHERE AccountId = :scMain.getId()
                 AND Escalation_Contact__c = true
                 ORDER BY Name ASC LIMIT 1000];
    }
jill.longojill.longo
I get this error when I try to create the Apex Class unexpected token: 'List' at Line 1 Column 7
Andy BoettcherAndy Boettcher
Jill,

(back in town)

Can you post your full code back on here?
jill.longojill.longo
Here's the code you provided. When I try to create the Class, I get an error that says "Invalid Constructor Name: getlstContacts at line 6 column 12". Step 1: Create a new Apex Class (your controller) public with Sharing class AccountEscalationContact { public ApexPages.standardController scMain {get;set;} public AccountEscalationContact(ApexPages.standardController sc) { scMain = sc; } public getlstContacts() { return [SELECT Id, Name, Phone FROM Contact WHERE AccountId = :scMain.getId() AND Escalation_Contact__c = true ORDER BY Name ASC LIMIT 1000]; } } Step 2: A quick change to your existing VF page: Step 3: Then you need a Unit Test (another Apex Class) to deploy everything to Production: @isTest private class UnitTest_AccountEscalationContact { @isTest static void testController() { Account acct = new Account(Name='Test Account'); insert acct; Contact conFalse = new Contact(AccountId=acct.Id); conFalse.Lastname = 'testfalse'; conFalse.Email = 'me@you.com'; conFalse.Escalation_Contact__c = false; insert conFalse; Contact conTrue = new Contact(AccountId=acct.Id); conTrue.Lastname = 'testtrue'; conTrue.Email = 'me@you.com'; conTrue.Escalation_Contact__c = true; insert conTrue; test.startTest(); AccountEscalationContact clsAEC = new AccountEscalationContact(new ApexPages.standardController(acct)); List lstContacts = clsAEC.getlstContacts(); System.Assert(lstContacts.size() == 1, 'did not find one escalation contact!'); test.stopTest(); } }