+ Start a Discussion
Mohan Raj 33Mohan Raj 33 

SObject row was retrieved via SOQL without querying the requested field: Account.Phone

I have get the above (title question)Error on the execution of my Controller and page.It's a standardcontroller with extension of to providing the pagination and sorting and now try to add the alpha  navigation bar.
So that's why I getting this error I don't know to how to rectify this error so can any one help me to solve the error.
My controller:
public class StandardPaginationSorting {

    // Variables required for Sorting.
    public String soql {get;set;}
    public List <Account> CandidateList1 = New List <Account>();
    public String soqlsort {get;set;}
    public List <Account> CandidateList2 = New List <Account>();
    public List<Account> acc {get; set;}

                // List used in to display the table in VF page.
                public List<Account> getCandidateList() {
                    // Passing the values of list to VF page.
                    return con.getRecords();
                    //all();
                }

                // instantiate the StandardSetController from a query locator
                public StandardPaginationSorting(ApexPages.StandardController controller){
                 con.getRecords();
                 all();
                }
                public ApexPages.StandardSetController con {
                    get {
                                                if(con == null) {
                                                                // String Query to have a list of cases for a respective End-user.
                                                                soql = 'SELECT Name, Website,BillingCountry, Phone, Type, Owner.Name FROM Account';

                                                                // Passing the String array to a list with Selected field sorting.
                                                                CandidateList1 = Database.query(soql + ' order by ' + sortField + ' ' + sortDir ); 

                                                                // setting values of List in StandardSetController.
                                                                con = new ApexPages.StandardSetController(CandidateList1);

                                                                // sets the number of records in each page set
                                                                con.setPageSize(10);
                                                }
                                                return con;
        }
        set;
    }

    // indicates whether there are more records after the current page set.
    public Boolean hasNext {
        get {
            return con.getHasNext();
        }
        set;
    }

    // indicates whether there are more records before the current page set.
    public Boolean hasPrevious {
        get {
            return con.getHasPrevious();
        }
        set;
    }

    // returns the page number of the current page set
    public Integer pageNumber {
        get {
            return con.getPageNumber();
        }
        set;
    }

    // returns the first page of records
    public void first() {
        con.first();
    }

    // returns the last page of records
    public void last() {
        con.last();
    }

    // returns the previous page of records
    public void previous() {
        con.previous();
    }

    // returns the next page of records
    public void next() {
        con.next();
    }

    // returns the PageReference of the original page, if known, or the home page.
    public void cancel() {
        con.cancel();
    }

    // Method for Constructor is used for Test Class.
    public StandardPaginationSorting(){ 
        //all();     
    }

   //Toggles the sorting of query from asc<-->desc
    public void toggleSort() {
        // simply toggle the direction
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';

                                // run the query again for sorting other columns
                                soqlsort = 'SELECT Name, Phone, BillingCountry, Website, Owner.Name, Type FROM Account'; 

                                // Adding String array to a List array
                                CandidateList2 = Database.query(soqlsort + ' order by ' + sortField + ' ' + sortDir ); 

                                // Adding Caselist to Standard Pagination controller variable
                                con = new ApexPages.StandardSetController(CandidateList2);

                                // Set Page Size to 10
                                con.setPageSize(10);

    }

    // the current sort direction. defaults to asc
    public String sortDir {
        // To set a Direction either in ascending order or descending order.
                                get  { if (sortDir == null) {  sortDir = 'asc';} return sortDir;}
        set;
    }

    // the current field to sort by. defaults to last name
    public String sortField {
        // To set a Field for sorting.
                                get  { if (sortField == null) {sortField = 'Name'; } return sortField;  }
        set;
    } 
    //the alpha bar navigation filter
    public PageReference ggg() {
        return null;
    }
    public PageReference eee() {
        return Null;
    }
    Public PageReference ddd() {
        return Null;
    }
    Public PageReference ccc() {
        return Null; 
    }
    Public PageReference bbb() {
        return Null;
    }
    
    string x;
    public PageReference fff() {
    x = 'f';
    acc.clear();
    String qry = 'SELECT  Name FROM Account WHERE Name LIKE \''+x+'%\' ORDER BY Name';
    acc= Database.query(qry);
    //con = new ApexPages.StandardSetController(acc);
        return null;
    }
    
    string xx;
    public PageReference rrr() {
     xx = 'R';
    acc.clear();
    String qry = 'SELECT  Name FROM Account WHERE Name LIKE \''+xx+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return null;
    }
    
    string z;
    public PageReference mmm() {
    z = 'm';
    acc.clear();
    String qry = 'SELECT  Name FROM Account WHERE Name LIKE \''+z+'%\' ORDER BY Name';
    acc= Database.query(qry);
    //con = new ApexPages.StandardSetController(acc);
        return null;
    }
    
    string y;
    public PageReference ooo() {
    y = 'o';
    acc.clear();
    String qry = 'SELECT  Name FROM Account WHERE Name LIKE \''+y+'%\' ORDER BY Name';
    acc= Database.query(qry);
        return null;
    }
    
    public void all() {
        acc = [SELECT Name FROM Account];
        con = new ApexPages.StandardSetController(acc);
        con.setpagesize(10);
    }
    
    public void aaa() {
        acc.clear();
        acc = [SELECT Name FROM Account];
        con = new ApexPages.StandardSetController(acc);
        con.setpagesize(10);
    }
    

}

For answer's thanks in advance.
Best Answer chosen by Mohan Raj 33
Dilip_VDilip_V
Mohan,

In some methods you are not querying the phone from account.Ex:mmm,all,aaa,ooo....etc.
So I added that field.
Give it a try
public class StandardPaginationSorting {

    // Variables required for Sorting.
    public String soql {get;set;}
    public List <Account> CandidateList1 = New List <Account>();
    public String soqlsort {get;set;}
    public List <Account> CandidateList2 = New List <Account>();
    public List<Account> acc {get; set;}

                // List used in to display the table in VF page.
                public List<Account> getCandidateList() {
                    // Passing the values of list to VF page.
                    return con.getRecords();
                    //all();
                }

                // instantiate the StandardSetController from a query locator
                public StandardPaginationSorting(ApexPages.StandardController controller){
                 con.getRecords();
                 all();
                }
                public ApexPages.StandardSetController con {
                    get {
                                                if(con == null) {
                                                                // String Query to have a list of cases for a respective End-user.
                                                                soql = 'SELECT Name, Website,BillingCountry, Phone, Type, Owner.Name FROM Account';

                                                                // Passing the String array to a list with Selected field sorting.
                                                                CandidateList1 = Database.query(soql + ' order by ' + sortField + ' ' + sortDir ); 

                                                                // setting values of List in StandardSetController.
                                                                con = new ApexPages.StandardSetController(CandidateList1);

                                                                // sets the number of records in each page set
                                                                con.setPageSize(10);
                                                }
                                                return con;
        }
        set;
    }

    // indicates whether there are more records after the current page set.
    public Boolean hasNext {
        get {
            return con.getHasNext();
        }
        set;
    }

    // indicates whether there are more records before the current page set.
    public Boolean hasPrevious {
        get {
            return con.getHasPrevious();
        }
        set;
    }

    // returns the page number of the current page set
    public Integer pageNumber {
        get {
            return con.getPageNumber();
        }
        set;
    }

    // returns the first page of records
    public void first() {
        con.first();
    }

    // returns the last page of records
    public void last() {
        con.last();
    }

    // returns the previous page of records
    public void previous() {
        con.previous();
    }

    // returns the next page of records
    public void next() {
        con.next();
    }

    // returns the PageReference of the original page, if known, or the home page.
    public void cancel() {
        con.cancel();
    }

    // Method for Constructor is used for Test Class.
    public StandardPaginationSorting(){ 
        //all();     
    }

   //Toggles the sorting of query from asc<-->desc
    public void toggleSort() {
        // simply toggle the direction
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';

                                // run the query again for sorting other columns
                                soqlsort = 'SELECT Name, Phone, BillingCountry, Website, Owner.Name, Type FROM Account'; 

                                // Adding String array to a List array
                                CandidateList2 = Database.query(soqlsort + ' order by ' + sortField + ' ' + sortDir ); 

                                // Adding Caselist to Standard Pagination controller variable
                                con = new ApexPages.StandardSetController(CandidateList2);

                                // Set Page Size to 10
                                con.setPageSize(10);

    }

    // the current sort direction. defaults to asc
    public String sortDir {
        // To set a Direction either in ascending order or descending order.
                                get  { if (sortDir == null) {  sortDir = 'asc';} return sortDir;}
        set;
    }

    // the current field to sort by. defaults to last name
    public String sortField {
        // To set a Field for sorting.
                                get  { if (sortField == null) {sortField = 'Name'; } return sortField;  }
        set;
    } 
    //the alpha bar navigation filter
    public PageReference ggg() {
        return null;
    }
    public PageReference eee() {
        return Null;
    }
    Public PageReference ddd() {
        return Null;
    }
    Public PageReference ccc() {
        return Null; 
    }
    Public PageReference bbb() {
        return Null;
    }
    
    string x;
    public PageReference fff() {
    x = 'f';
    acc.clear();
    String qry = 'SELECT  Name,phone  FROM Account WHERE Name LIKE \''+x+'%\' ORDER BY Name';
    acc= Database.query(qry);
    //con = new ApexPages.StandardSetController(acc);
        return null;
    }
    
    string xx;
    public PageReference rrr() {
     xx = 'R';
    acc.clear();
    String qry = 'SELECT  Name,phone  FROM Account WHERE Name LIKE \''+xx+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return null;
    }
    
    string z;
    public PageReference mmm() {
    z = 'm';
    acc.clear();
    String qry = 'SELECT  Name,phone  FROM Account WHERE Name LIKE \''+z+'%\' ORDER BY Name';
    acc= Database.query(qry);
    //con = new ApexPages.StandardSetController(acc);
        return null;
    }
    
    string y;
    public PageReference ooo() {
    y = 'o';
    acc.clear();
    String qry = 'SELECT  Name,phone  FROM Account WHERE Name LIKE \''+y+'%\' ORDER BY Name';
    acc= Database.query(qry);
        return null;
    }
    
    public void all() {
        acc = [SELECT Name,phone FROM Account];
        con = new ApexPages.StandardSetController(acc);
        con.setpagesize(10);
    }
    
    public void aaa() {
        acc.clear();
        acc = [SELECT Name,phone FROM Account];
        con = new ApexPages.StandardSetController(acc);
        con.setpagesize(10);
    }
    

}

Let us know if you have any issues.

Mark it as best answer if it works.

Thanks.

All Answers

Dilip_VDilip_V
Mohan,

In some methods you are not querying the phone from account.Ex:mmm,all,aaa,ooo....etc.
So I added that field.
Give it a try
public class StandardPaginationSorting {

    // Variables required for Sorting.
    public String soql {get;set;}
    public List <Account> CandidateList1 = New List <Account>();
    public String soqlsort {get;set;}
    public List <Account> CandidateList2 = New List <Account>();
    public List<Account> acc {get; set;}

                // List used in to display the table in VF page.
                public List<Account> getCandidateList() {
                    // Passing the values of list to VF page.
                    return con.getRecords();
                    //all();
                }

                // instantiate the StandardSetController from a query locator
                public StandardPaginationSorting(ApexPages.StandardController controller){
                 con.getRecords();
                 all();
                }
                public ApexPages.StandardSetController con {
                    get {
                                                if(con == null) {
                                                                // String Query to have a list of cases for a respective End-user.
                                                                soql = 'SELECT Name, Website,BillingCountry, Phone, Type, Owner.Name FROM Account';

                                                                // Passing the String array to a list with Selected field sorting.
                                                                CandidateList1 = Database.query(soql + ' order by ' + sortField + ' ' + sortDir ); 

                                                                // setting values of List in StandardSetController.
                                                                con = new ApexPages.StandardSetController(CandidateList1);

                                                                // sets the number of records in each page set
                                                                con.setPageSize(10);
                                                }
                                                return con;
        }
        set;
    }

    // indicates whether there are more records after the current page set.
    public Boolean hasNext {
        get {
            return con.getHasNext();
        }
        set;
    }

    // indicates whether there are more records before the current page set.
    public Boolean hasPrevious {
        get {
            return con.getHasPrevious();
        }
        set;
    }

    // returns the page number of the current page set
    public Integer pageNumber {
        get {
            return con.getPageNumber();
        }
        set;
    }

    // returns the first page of records
    public void first() {
        con.first();
    }

    // returns the last page of records
    public void last() {
        con.last();
    }

    // returns the previous page of records
    public void previous() {
        con.previous();
    }

    // returns the next page of records
    public void next() {
        con.next();
    }

    // returns the PageReference of the original page, if known, or the home page.
    public void cancel() {
        con.cancel();
    }

    // Method for Constructor is used for Test Class.
    public StandardPaginationSorting(){ 
        //all();     
    }

   //Toggles the sorting of query from asc<-->desc
    public void toggleSort() {
        // simply toggle the direction
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';

                                // run the query again for sorting other columns
                                soqlsort = 'SELECT Name, Phone, BillingCountry, Website, Owner.Name, Type FROM Account'; 

                                // Adding String array to a List array
                                CandidateList2 = Database.query(soqlsort + ' order by ' + sortField + ' ' + sortDir ); 

                                // Adding Caselist to Standard Pagination controller variable
                                con = new ApexPages.StandardSetController(CandidateList2);

                                // Set Page Size to 10
                                con.setPageSize(10);

    }

    // the current sort direction. defaults to asc
    public String sortDir {
        // To set a Direction either in ascending order or descending order.
                                get  { if (sortDir == null) {  sortDir = 'asc';} return sortDir;}
        set;
    }

    // the current field to sort by. defaults to last name
    public String sortField {
        // To set a Field for sorting.
                                get  { if (sortField == null) {sortField = 'Name'; } return sortField;  }
        set;
    } 
    //the alpha bar navigation filter
    public PageReference ggg() {
        return null;
    }
    public PageReference eee() {
        return Null;
    }
    Public PageReference ddd() {
        return Null;
    }
    Public PageReference ccc() {
        return Null; 
    }
    Public PageReference bbb() {
        return Null;
    }
    
    string x;
    public PageReference fff() {
    x = 'f';
    acc.clear();
    String qry = 'SELECT  Name,phone  FROM Account WHERE Name LIKE \''+x+'%\' ORDER BY Name';
    acc= Database.query(qry);
    //con = new ApexPages.StandardSetController(acc);
        return null;
    }
    
    string xx;
    public PageReference rrr() {
     xx = 'R';
    acc.clear();
    String qry = 'SELECT  Name,phone  FROM Account WHERE Name LIKE \''+xx+'%\' ORDER BY Name';
    acc= Database.query(qry);
    con = new ApexPages.StandardSetController(acc);
        return null;
    }
    
    string z;
    public PageReference mmm() {
    z = 'm';
    acc.clear();
    String qry = 'SELECT  Name,phone  FROM Account WHERE Name LIKE \''+z+'%\' ORDER BY Name';
    acc= Database.query(qry);
    //con = new ApexPages.StandardSetController(acc);
        return null;
    }
    
    string y;
    public PageReference ooo() {
    y = 'o';
    acc.clear();
    String qry = 'SELECT  Name,phone  FROM Account WHERE Name LIKE \''+y+'%\' ORDER BY Name';
    acc= Database.query(qry);
        return null;
    }
    
    public void all() {
        acc = [SELECT Name,phone FROM Account];
        con = new ApexPages.StandardSetController(acc);
        con.setpagesize(10);
    }
    
    public void aaa() {
        acc.clear();
        acc = [SELECT Name,phone FROM Account];
        con = new ApexPages.StandardSetController(acc);
        con.setpagesize(10);
    }
    

}

Let us know if you have any issues.

Mark it as best answer if it works.

Thanks.
This was selected as the best answer
Mohan Raj 33Mohan Raj 33
@Thermo Dynamics Thanks for the reply it's rectified my headache of the error of the program.