+ Start a Discussion
DMaloneDMalone 

Trailhead: Creating & Using Custom Controllers error

I'm on the second to last unit of the Visualforce Basics module, called Creating & Using Custom Controllers.
I THINK I have followed everything correctly (being new to coding) but I'm getting an error when in the section Add A New Action Method:

line 7: expecting a semi-colon, found "("


The custom controller I am trying to use is as follows and the line in bold is line 7:

public class ContactsListController {
    private String sortOrder = 'LastName';
        public List<Contact> getContacts() {
    public void sortByLastName() {
    this.sortOrder = 'LastName';
}    
public void sortByFirstName() {
    this.sortOrder = 'FirstName';
}   
        List<Contact> results = Database.query(
            'SELECT Id, FirstName, LastName, Title, Email ' +
            'FROM Contact ' +
            'ORDER BY ' + sortOrder + ' ASC ' +
            'LIMIT 10'
        );
        return results;
    }
}


Any ideas on where I have gone wrong?

Thanks
Dan
Best Answer chosen by DMalone
Andy BoettcherAndy Boettcher
Perfect!  That makes it WAY easier to read.  =)

Try this:
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;
    }

public void sortByLastName() {
    this.sortOrder = 'LastName';
}
    
public void sortByFirstName() {
    this.sortOrder = 'FirstName';
}

}

 

All Answers

Andy BoettcherAndy Boettcher
Can you post your code again using the code formatter?  "< >" button in the composer here.
DMaloneDMalone
public class ContactsListController {

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

}

Apologies.
How's this? Long term admin, new to dev stuff :)
Andy BoettcherAndy Boettcher
Perfect!  That makes it WAY easier to read.  =)

Try this:
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;
    }

public void sortByLastName() {
    this.sortOrder = 'LastName';
}
    
public void sortByFirstName() {
    this.sortOrder = 'FirstName';
}

}

 
This was selected as the best answer
DMaloneDMalone
I had to do one little amendment, add two brackets after getContacts on line 5
public List<Contact> getContacts () {

Now no more errors! :)
I misread the instructions I think and got things in the wrong order. Thank you very much for you help Andy, much appreciated.
Now I can enjoy Christmas in peace ;)

Dan