Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
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
5 answers
  1. Dec 15, 2015, 3:54 PM
    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';

    }

    }

     
Loading
0/9000