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
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
Rob Koch 1Rob Koch 1
Thank you Dan for the question, and Andy for the answer. This helped another newbie coder 5 years later. 
Will send feedback to Trailhead on this one. The instructions for WHERE to add the two action methods (sort by first name, sort by last name) were confusing. After the Get Contacts method is where it goes, but not right after that line. Those methods aren't added until after the return results section and right before the closing bracket.