SOQL (Salesforce Object Query Language)
SOQL:
- SOQL (Salesforce Object Query Language) retrieves the records from the database by using a SELECT keyword.
- SOQL allows you to specify the source object (such as Account), a list of fields to retrieve, and conditions for selecting rows in the source object.
- SOQL doesn’t support all advanced features of the SQL SELECT command.
- The syntax of SOQL, SELECT one or more fields FROM object WHERE filter statements and, optionally, results are ordered.
- SOQL query is enclosed between square brackets. For example, Account a = [Select ID, Name from Account where Name=’acc1′]; In the above query “a” variable stores the ID, Name of the all accounts with a name “acc1”.
For Parent-Child SOQL:
I have Contact as a Parent object & Account as a Child object.
for(Contact c : [select id,accountId from Contact where Contact.AccountId =: '0010K00001cqUdv']){
for(Account ac : [select name,description from Account where id =: c.accountId]){
Contact con = new Contact();
con.id = c.id;
con.lastname = ac.name;
con.description = ac.description;
System.debug(con);
}
}
For Child-Parent SOQL:
I have Contact as a Parent object & Account as a Child object.
for(Contact c:[select lastname,Account.Name from Contact])
{
system.debug('Contact LastName: '+c.lastname+' Account Name: '+c.Account.Name);
}
Comments
Post a Comment