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
Melissa HowardMelissa Howard 

Variables in VF page not being used properly in APEX

Hi,

I want to do a lookup based on 3 contact input fields in the VF page. So, I created variables in the VF that the APEX controller can use in it's SELECT statement. But, the variables created in the VF page give an error.

Here is the APEX:

public with sharing class ContactExtendedController2 {

    public Contact contactSetting{get;set;}

    public ContactExtendedController2(ApexPages.StandardController Contact) {
        contactSetting=new Contact();
        contactSetting = [SELECT Id, Name FROM Contact
            WHERE FirstName = firstname AND LastName = lastname]; // AND LastName = lastName];
                               // WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }      
 
The error in compiling this code is:
Error: Compile Error: expecting a colon, found 'firstname' at line 8 column 30  

Here is the VF, which saves and previews fine,  that sets the firstName and lastName variables that I want to use in the APEX


					
<apex:page standardController="Contact" extensions="ContactExtendedController2">

    <apex:form >
        <apex:pageBlock title="Foodbank of Santa Barbara County Volunteer Information">
            <apex:pageBlockSection columns="1">
                <!-- <b>Input Date  :</b><apex:inputfield value="TODAY"/> This gives error, even if value="7/21/2014" -->
                <apex:outputLabel value="Lookup Contact" for="ContactLookup"/>
                <apex:inputField id="RequestedBy" value="{!contactSetting.Name}" />
                <apex:inputField value="{!Contact.FirstName}"/> 
                <apex:inputField value="{!Contact.LastName}"/> 
                <apex:inputField value="{!Contact.Email}"/> 
                
                    <apex:variable var="firstName" value="{!Contact.FirstName}" />
                    <apex:variable var="lastName" value="{!Contact.LastName}" />
         
   
James LoghryJames Loghry
You have a couple issues, first is your SOQL syntax is attempting to bind variables (you will need a colon before firstname and a colon before lastname.  An example is below:

[SELECT Id, Name FROM Contact WHERE FirstName = :firstname AND LastName = :lastname];
However, firstname, lastname, and other standard fields are available already, in your visualforce and StandardController, so for instance you can use the StandardController instance's "getRecord" method to get your contact.

public ContactExtendedController2(ApexPages.StandardController ctrl) {
        contactSetting= (Contact)ctrl.getRecord();
        //If you need fields not available via the standard controller, you can requery with the Id like below
        //Id contactId = ctrl.getId();
        //contactSetting = [Select Firstname,Lastname,Name From Contact Where Id = contactId];
}



James LoghryJames Loghry
Here's a doc on how SOQL variables work: https://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_variables.htm
~Onkar~Onkar
Only issue with your code is using vatiable without colon  " : "


public with sharing class ContactExtendedController2 {

    public Contact contactSetting{get;set;}

    public ContactExtendedController2(ApexPages.StandardController Contact) {
        contactSetting=new Contact();
        contactSetting = [SELECT Id, Name FROM Contact
            WHERE FirstName = firstname AND LastName = lastname]; // AND LastName = lastName];

                               // WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }


// Correct Query.
contactSetting = [SELECT Id, Name FROM Contact
            WHERE FirstName =: firstname AND LastName =: lastname]; // AND LastName = lastName];



~Thanks
Onkar Kumar
Ramakrishnan AyyanarRamakrishnan Ayyanar
your query is only wrong:

please use it: 
contactSetting =[SELECT Id, Name FROM Contact WHERE FirstName =: firstname AND LastName =: lastname];