You need to sign in to do that
Don't have an account?

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:
Here is the VF, which saves and previews fine, that sets the firstName and lastName variables that I want to use in the APEX
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}" /> | |
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 with sharing class ContactExtendedController2 {
public Contact contactSetting{get;set;}
public ContactExtendedController2(ApexPages.StandardController Contact) {
contactSetting=new Contact();
contactSetting = [SELECT Id, Name FROM ContactWHERE 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
please use it:
contactSetting =[SELECT Id, Name FROM Contact WHERE FirstName =: firstname AND LastName =: lastname];