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
dev.shiv1.3478599680068948E12dev.shiv1.3478599680068948E12 

Retrieving fields from the soql query

How to retrieve the account fields from the query [SELECT Accoun.Id,Account.Name FROM Contact Limit 5] in a class.

The code is shown below...

public class Retrieving_Fields {
public List<Contact> con{set;get;}
public void RetrievingFields(){
con=[SELECT Account.Id,Account.Name,Account.Phone FROM Contact LIMIT 5];
System.debug(con);
}
}

please hlelp me out how to retrieve those fields from the above class

Best Answer chosen by Admin (Salesforce Developers) 
Janet GuoJanet Guo

dev.shiv, what functionality is it that you are looking for? sourav046's and Madhan's suggestions are both valid solutions, but they don't return the same thing. 

 

In sourav046's case, a list of up to 5 Contacts are being returned for ONE specific Account that matches the Account Id given. Note: You don't need to declare an array of Accounts to query for their Contacts. (Also, btw, in a SOQL query, variable names need to be prefaced with a colon, so it should be "Account.ID = :a.ID").

 

In Madhan's case, he's returning a 5 Contacts from ANY Accounts, just five total Contacts, not tied one Account in particular. 

 

Also, another area that might cause a problem is what happens if the query returns no results? You might want to have a try-catch block to deal with those kinds of situations.

All Answers

sourav046sourav046

First you need to take a array of Accounts :

 

List<Account> acc=new List<Account>();

Account a=new Account();

a=[select Id from Account];

 

 

Then take a array of Contact :

List<Contact> con=new List<Contact>();

Contact c=[SELECT Accoun.Id,Account.Name FROM Contact where Account.ID=a.ID Limit 5 ];

 

dev.shiv1.3478599680068948E12dev.shiv1.3478599680068948E12

Its not working. Its executing but throwing an execption as "List has more than 1 row for assignment to sObject". Could do please give me any solution for that...

Madhan Raja MMadhan Raja M

Try this out:

 

List<Contact> cont=new List<Contact>();

cont=[SELECT Accoun.Id,Account.Name FROM Contact where Account.ID=a.ID Limit 5 ];

Madhan Raja M

dev.shiv1.3478599680068948E12dev.shiv1.3478599680068948E12

Hi Mr.Madhan. The thing is how to retrieve the account field values from contact object through apex class. If you run the apex class with the the above query it just displays Account Id instead of the field values. Could u please suggest how to write the query for that?

sourav046sourav046

If you dont declare what is 'a' (which is actually an instatnce of Account) it will return 'variable not found' error .

Madhan Raja MMadhan Raja M

cont=[SELECT Account.Id,Account.Name FROM Contact Limit 5 ];

List the Account fields that are required for you in the SOQL statement.

 

Madhan Raja M

Madhan Raja MMadhan Raja M

@Sourav: 'a' is an addon from your post.

 

Madhan Raja M

sourav046sourav046

I think it is only possible if we retreive a query on Account first .Without quering Account how can we get that 'a' ?

Madhan Raja MMadhan Raja M

Hi @Sourav

 

The follwing SOQL work because, the contact object has a relationship with account. 

cont=[SELECT Account.Id,Account.Name FROM Contact Limit 5 ];

 

So Without quering Account we can get the details.

Note:  The above SOQL will not fetch any data if the contact is not associated with account.

 

Madhan Raja M

 

 

 

 

Janet GuoJanet Guo

dev.shiv, what functionality is it that you are looking for? sourav046's and Madhan's suggestions are both valid solutions, but they don't return the same thing. 

 

In sourav046's case, a list of up to 5 Contacts are being returned for ONE specific Account that matches the Account Id given. Note: You don't need to declare an array of Accounts to query for their Contacts. (Also, btw, in a SOQL query, variable names need to be prefaced with a colon, so it should be "Account.ID = :a.ID").

 

In Madhan's case, he's returning a 5 Contacts from ANY Accounts, just five total Contacts, not tied one Account in particular. 

 

Also, another area that might cause a problem is what happens if the query returns no results? You might want to have a try-catch block to deal with those kinds of situations.

This was selected as the best answer