You need to sign in to do that
Don't have an account?
dev.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
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
First you need to take a array of Accounts :
Then take a array of Contact :
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...
Try this out:
Madhan Raja M
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?
If you dont declare what is 'a' (which is actually an instatnce of Account) it will return 'variable not found' error .
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
@Sourav: 'a' is an addon from your post.
Madhan Raja M
I think it is only possible if we retreive a query on Account first .Without quering Account how can we get that 'a' ?
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
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.