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
aditya3aditya3 

Soql Query

Hi all,
I am trying to query all the records in Account and Contact object ,As I have tried Like this
public class hong12
 {
public List<account> acclist{get;set;}
public List<contact> contlist{get;set;}
public hong12()
{
acclist=[select id,Name,phone,Industry,Type,NumberOfEmployees from Account];
contlist=[select id,Name,Email,phone from Contact];
}
}But I am getting Error Like this
Error: Compile Error: The property List<Contact> contlist is referenced by Visualforce Page (newhomepg) in salesforce.com. Remove the usage and try again. at line 4 column 22
 
Best Answer chosen by aditya3
Sampath KumarSampath Kumar
Hi Aditya,

In your apex class where there is contlist=[select id,Name,Email,phone from Contact]; modify it to include Account in it.
 contlist=[select id,Name,Account,Email,phone from Contact]; 

As per the error, you are not retrieving the account from contact but you are using it in visualforce page,

Let me know if you have any issues after doing this.

Regards
Sampath Kumar Goud

All Answers

Sampath KumarSampath Kumar
Hi Aditya,

The error is because, you are modifying the existing class which is used by visualforce page newhomepg. To resolve this issue, comment the code which uses contlist in visualforce page newhomepg, save it then save the apex class and uncomment the code in visualforce page.

Mark this as best answer if this solves your issue.

Regards
Sampath Kumar Goud
aditya3aditya3
Hi Sampath,

System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.Account

THis error came,I have updated the above What you told
 
RishavRishav
Hii,
I think u r trying to use {!contact.AccountId} in your visualforce page. But You hve not queried that one in your "Cont" list.  
Sampath KumarSampath Kumar
Hi Aditya,

In your apex class where there is contlist=[select id,Name,Email,phone from Contact]; modify it to include Account in it.
 contlist=[select id,Name,Account,Email,phone from Contact]; 

As per the error, you are not retrieving the account from contact but you are using it in visualforce page,

Let me know if you have any issues after doing this.

Regards
Sampath Kumar Goud
This was selected as the best answer
ManojjenaManojjena
Hi ,

conList=[SELECT Id,Name,Account.Name,AccountId,Email,Phone FROM Contact];

Here in this query you can get AccontId and Account Name .If you need id keep AccountId ,if you need Account Name then keep Account.Name .

 
aditya3aditya3
Thanks@Manoj Kumar Jena and @Rishav