+ Start a Discussion
Raksha TiwariRaksha Tiwari 

System.QueryException: unexpected token: 10

This is my controller :

public class ContactsListController{

 private string sortOrder = 'LastName';
 
 public List<Contact> getContacts(){
 
  string soqlQuery = 'SELECT Id, FirstName, LastName, Title, Email'+ 'from Contacts'+ 'OrderBy' + sortOrder 
                          + 'ASC' + 'LIMIT 10' ;
  
  List<Contact> results = Database.query(soqlQuery);
  return results;
  
 }                         

}



And this is my visualforce page :

<apex:page controller="ContactsListController" >
  <apex:form >
   <apex:pageBlock title="Contacts List" id="Contacts_list">
   
    <apex:pageBlockTable value="{!Contacts}" var="ct">
    <apex:column value="{!ct.FirstName}"/>
    <apex:column value="{!ct.LastName}"/>
    <apex:column value="{!ct.Title}"/>
    <apex:column value="{!ct.Email}"/>
    
    </apex:pageBlockTable>
  
   </apex:pageBlock>
  </apex:form>
</apex:page>


I'm getting an error "System.QueryException: unexpected token: 10 " while saving the above vf page. Please help.
Best Answer chosen by Raksha Tiwari
Sai Ram ASai Ram A
Hi Sourabh
 
public class ContactsListController{

private string sortOrder = 'LastName';

	public List<Contact> getContacts(){

	string soqlQuery = 'SELECT Id, FirstName, LastName, Title, Email from Contact ORDER BY Name ASC LIMIT 10' ;

	system.debug('String Query========> '+soqlQuery);

	List<Contact> results = Database.query(soqlQuery);

	return results;

	}                         

}

Note : Please mark this answer as Best Answer if it helps you in giving a surface understanding about your problem. Doing this, will help the community to directly refer to this answer

Thank you
Blearn-Sai

All Answers

Facebook IntegrationFacebook Integration
public class ContactsListController{

// private string sortOrder = 'LastName';
 
 public List<Contact> getContacts(){
 
  /*string soqlQuery = 'SELECT Id, FirstName, LastName, Title, Email'+ 'from Contacts'+ 'OrderBy' + sortOrder 
                          + 'ASC' + 'LIMIT 10' ;*/
                  
  String soqlQuery= 'select id,FirstName,LastName,Title,Email from Contacts ORDERBY LastName ASC LIMIT 10';
  
  List<Contact> results = Database.query(soqlQuery);
  return results;
  
 }                         

}

try this may help u
 
Sai Ram ASai Ram A
Hi Sourabh
 
public class ContactsListController{

private string sortOrder = 'LastName';

	public List<Contact> getContacts(){

	string soqlQuery = 'SELECT Id, FirstName, LastName, Title, Email from Contact ORDER BY Name ASC LIMIT 10' ;

	system.debug('String Query========> '+soqlQuery);

	List<Contact> results = Database.query(soqlQuery);

	return results;

	}                         

}

Note : Please mark this answer as Best Answer if it helps you in giving a surface understanding about your problem. Doing this, will help the community to directly refer to this answer

Thank you
Blearn-Sai
This was selected as the best answer
Hanumantha Rao MuddanaHanumantha Rao Muddana
If you print your quey it will be like below:
SELECT Id, FirstName, LastName, Title, Emailfrom ContactsOrderByLastNameASCLIMIT 10
If you observe this carefully you can see spaces are missing. Take care of space after each word.
'OrderBy' should  be 'Order By'.

I believe you are trying to query standard object Contact. Change 'Contacts' to 'Contact'

Hope this helps :)

 
sfdcdevsfdcdev
public List<Contact> getContacts() {
    
    List<Contact> results = Database.query(
        'SELECT Id, FirstName, LastName, Title, Email ' +
        'FROM Contact ' +
        'ORDER BY ' + sortOrder + ' ASC ' +
        'LIMIT 10'
    );
    
        return results;
    }

 
Andrea IanniAndrea Ianni
Actually your error was very very simple: a space missing!
Change your query: 
string soqlQuery = 'SELECT Id, FirstName, LastName, Title, Email'+ 'from Contacts'+ 'OrderBy' + sortOrder 
                          + 'ASC' + 'LIMIT 10' ;

with the following and it will work properly.
string soqlQuery = 'SELECT Id, FirstName, LastName, Title, Email '+ ' from Contacts '+ ' OrderBy ' + sortOrder 
                          + ' ASC ' + ' LIMIT 10 ' ;

Your code was "almost" right. :)