Welcome to Support!

Search for an answer or ask a question of the zone or Customer Support.

Need help? Dismiss Show All Questions sorted by Date Posted

Show

sorted by

Parteek Goyal 3 Parteek Goyal 3

System.QueryException: unexpected token: '<EOF>'

Hi All,

I am trying to write search method but i have got this error.
System.QueryException: unexpected token: '<EOF>'.
public class contactSave{ public Contact cont{get;set;} public Contact contList{get;set;} public List<Contact> listContact{get;set;} public contactSave(ApexPages.StandardController controller){ contList=(Contact)controller.getRecord(); // varprofile=(Profile__c)controller.getRecord(); cont=new Contact(); public void savemethod(){ List<Contact> con=[SELECT id,firstname,lastname,Email from Contact where firstname=:cont.firstname and lastname=:cont.lastname and Email=:cont.Email]; if(con.size()>0){ ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'Record Allready Exists')); else{ insert cont; ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'Record Inserted')); public PageReference searchm(){ String querystring='SELECT id,firstname,lastname,email from Contact Where'; if(contList.firstname!=null && contList.lastname==null && contList.Email==null){ querystring += 'firstname =\''+cont.firstname+'\''; }else if(contList.firstname!=null && contList.lastname!=null && contList.Email==null){ querystring += 'firstname =\''+contList.firstname+'\'' +'and'+' '+'lastname =\''+contList.lastname+'\''; }else if(contList.firstname!=null && contList.lastname!=null && contList.Email!=null){ querystring += 'firstname =\''+contList.firstname+'\'' +'and'+' '+'lastname =\''+contList.lastname+'\'' +'and'+' '+'Email =\''+contList.Email+'\''; }else if(contList.firstname==null && contList.lastname!=null && contList.Email!=null){ querystring += 'lastname =\''+contList.lastname+'\'' +'and'+' '+'Email =\''+contList.Email+'\''; }else if(contList.firstname==null && contList.lastname==null && contList.Email!=null){ querystring += 'Email =\''+contList.Email+'\''; }else if(contList.firstname!=null && contList.lastname==null && contList.Email!=null){ querystring += 'firstname =\''+contList.firstname+'\'' +'and'+' '+'Email =\''+contList.Email+'\''; listContact=Database.query(querystring); System.debug('Querystring>>>>'+ querystring); return null;
VF Page

<apex:page StandardController="Contact" extensions="contactSave">
<apex:form >
<apex:pageblock >
<apex:pagemessages />
<apex:pageblockButtons >
<apex:commandButton value="Save" action="{!savemethod}"/>
<apex:commandButton value="Search" action="{!searchm}"/>
</apex:pageblockButtons>
<apex:pageblockSection >
<apex:inputfield value="{!cont.firstname}"/>
<apex:inputfield value="{!cont.lastname}"/>
<apex:inputfield value="{!cont.Email}"/>
</apex:pageblockSection>
<apex:pageblockTable value="{!listContact}" var="lc">
<apex:column value="{!lc.firstname}"/>
</apex:pageblockTable>
</apex:pageblock>
</apex:form>
</apex:page>

Thanak in advance...
sandeep sankhla sandeep sankhla
Hi Parteek,

The error is in your query string...If in ytour searchChm method control will not go inside any if condition then your final query string will be


Querystring>>>>SELECT id,firstname,lastname,email from Contact Where

Above string has no meaning because there is nothing after where ..

you can simply check in the end if there is nothing after where then remove where also to resolve the error..

Please check and let me know if this helps you

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer


sandeep sankhla sandeep sankhla
Hi Parteek,

You can simply replace your method with this method
public PageReference searchm(){ String querystring='SELECT id,firstname,lastname,email from Contact'; if(contList.firstname!=null && contList.lastname==null && contList.Email==null){ querystring += ' where firstname =\''+cont.firstname+'\''; }else if(contList.firstname!=null && contList.lastname!=null && contList.Email==null){ querystring += ' where firstname =\''+contList.firstname+'\'' +'and'+' '+'lastname =\''+contList.lastname+'\''; }else if(contList.firstname!=null && contList.lastname!=null && contList.Email!=null){ querystring += ' where firstname =\''+contList.firstname+'\'' +'and'+' '+'lastname =\''+contList.lastname+'\'' +'and'+' '+'Email =\''+contList.Email+'\''; }else if(contList.firstname==null && contList.lastname!=null && contList.Email!=null){ querystring += ' where lastname =\''+contList.lastname+'\'' +'and'+' '+'Email =\''+contList.Email+'\''; }else if(contList.firstname==null && contList.lastname==null && contList.Email!=null){ querystring += ' where Email =\''+contList.Email+'\''; }else if(contList.firstname!=null && contList.lastname==null && contList.Email!=null){ querystring += ' where firstname =\''+contList.firstname+'\'' +'and'+' '+'Email =\''+contList.Email+'\''; System.debug('Querystring>>>>'+ querystring); listContact=Database.query(querystring); System.debug('Querystring>>>>'+ querystring); return null;
I have removed where clause from main string and added where inside the loop..
Now as you have used else if ..so it will always go inside one if condition so no need to check and remove where..

You can simply replace your method wiuth above code, it will work

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer
David Zhu David Zhu
Hi,
You just need to add a space after 'where' at the line 22. Otherewise, the soql would be like 'select .... from contact wherefirstname....'.

String querystring='SELECT id,firstname,lastname,email from Contact Where ';

David
sandeep sankhla sandeep sankhla
Hi Parteek,

I agree with David, if control will go in any if condition then due to space it can give the same eror, but if it will not go then du eto space it can cause the same issue..

So the best solution is put where inside the conditions and provide the space..if you notice I haev provided the space before where clause to avoid spacing issue..

Please repalce your code and let us know if that helps you..

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer