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
Brooks Johnson 6Brooks Johnson 6 

Trailhead Custom Controllers: Unexpected Token.

Hi I am trying to work through the Custom Controller challenge in Trailhead.  My VF page is giving me the error : 
Visualforce Error
Help for this Page
System.QueryException: unexpected token: ORDER 
Class.ContactsListController.getContacts: line 7, column 1

I am not seeing anything wrong with my code.  Any thoughts on where I am going wrong?
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>


public class ContactsListController {
    
    private String sortOrder = 'LastName';
    
    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;
    
            }
}

 
Best Answer chosen by Brooks Johnson 6
Steven NsubugaSteven Nsubuga
Add a space between Email and FROM in the query. Seems the query now is SELECT Id, FirstName, LastName, Title, EmailFROM Contact

All Answers

Steven NsubugaSteven Nsubuga
Add a space between Email and FROM in the query. Seems the query now is SELECT Id, FirstName, LastName, Title, EmailFROM Contact
This was selected as the best answer
Ajay K DubediAjay K Dubedi

Hi Brooks,

Please try the below code for VF Page
 

//VF 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>

//Apex Controller

public class ContactsListController {
    
    private String sortOrder = 'LastName';
   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;
    
    }
}
Please mark it as Best if it helps you.

Thanks,
Ajay
Brooks Johnson 6Brooks Johnson 6
Got it. Thanks, everyone.