4

I have a visualforce page that returns a list of Contacts and Leads, I want a column with the name of the Contact or the Lead whatever is in the list,I have a formula field IsContact__c that returns true if the campaign member is a Contact and false if it is a Lead. Here is the markup:

<apex:pageBlockTable value="{!Members}" var="c"> 
    <apex:column value="{!IF(c.IsContact__c,c.Contact.Name,c.Lead.Name)}"/>

When I run it, it gives me this error message:

Syntax error. Missing ')' Error is in expression '{!IF(c.IsContact__c,c.Contact.Name,c.Lead}' in component in page addmemberpage

6
  • Your error message syntax does not match your markup... are you sure you correctly copied the markup correctly?
    – Adrian Larson
    Commented May 26, 2015 at 19:57
  • I did, This is more of the code: <apex:pageBlock > <apex:pageBlockSection columns="1" > <apex:pageBlockTable value="{!Members}" var="c"> <apex:column value="{!IF(c.IsContact__c,c.Contact.Name,c.Lead.Name)}"/> </apex:pageBlockTable>
    – Leo
    Commented May 26, 2015 at 19:58
  • Add some controller code also? Is Members a list of CampaignMember?
    – Adrian Larson
    Commented May 26, 2015 at 19:59
  • And you also copied the error message correctly? Very strange.
    – Adrian Larson
    Commented May 26, 2015 at 20:00
  • I agree. Is there another way of doing an If statement in Visualforce?
    – Leo
    Commented May 26, 2015 at 20:02

2 Answers 2

3

Change

{!IF(c.IsContact__c,c.Contact.Name,c.Lead}

to

{!IF(c.IsContact__c,c.Contact.Name,c.Lead)}

If you cannot resolve the mismatch, you could create a wrapper class to loop over. Without seeing your existing controller/extesnion, it would look something like:

public class MemberWrapper
{
    public CampaignMember record { get; private set; }
    public MemberWrapper(CampaignMember record)
    {
        this.record = record;
    }
    public String name
    {
        get
        {
            return record.IsContact__c ? record.Contact.Name : record.Lead.Name;
        }
    }
}
1
  • This comment is just out of curosity the code he posted is having the closing bracket. but the error message is missing. what would be the reason.
    – Suresh
    Commented May 26, 2015 at 20:03
1

I got it to work, you have to use rendered in order to use IF "show this" ELSE "Show THAT" This is the new code:

<apex:pageBlockTable value="{!Members}" var="c">
                <apex:column ><apex:facet name="header">Name</apex:facet>
                <apex:outputText value="{!c.Contact.Name}" rendered="{!c.IsContact__c==True}" />
                <apex:outputText value="{!c.Lead.Name}" rendered="{!c.IsContact__c==False}" />
               </apex:column>

Now when IsContact is true the column Name will show the name of the Contact and when IsContact is false it will show the Lead Name. Thanks for your help guys!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .