Professional Documents
Culture Documents
to
&
DML
SOQL
SOSL
// Create the account sObject
Account acct = new Account(Name='Acme', Phone='(415)555-1212',
NumberOfEmployees=100);
// Insert the account by using DML
insert acct;
// Get the new ID on the inserted sObject argument
ID acctID = acct.Id;
// Display this ID in the debug log
System.debug('ID = ' + acctID);
insert acct;
} catch (DmlException e) {
1. interviewer__c.position__r.status__c = ‘open’;
2. position__c.interviewers (just a collection to be iterated over)
3. review__c.job_application__r.position__r.status__c =
‘closed’;
You can traverse a relationship from a child
object (contact) to a field on its parent
(Account.Name) by using dot notation.
◦ SELECT Id, Account.Name, Account.Industry,
Account.Website
FROM Contact
WHERE Account.NumberOfEmployees >= 200
◦ SELECT Account.Owner.Profile.CreatedBy.Name
FROM Contact
To get child records related to a parent record, add an
inner query for the child records.
For example, if we’re doing a SOQL query on accounts,
downwards traversal could pull data from the account’s
contacts.
◦ SELECT Id, Name, Industry, AnnualRevenue, ( SELECT Name,
Email, BirthDate FROM Contacts ) FROM Account
◦ The nested query is treated like another field. That’s why there
is a comma after the AnnualRevenue field.
◦ We use the plural version “Contacts” in the nested SOQL query.
If you need to find the keyword for a custom relationship, find
the lookup or master-detail field and look in the “Child
Relationship Name” field on the related field of the child object
(Account Lookup field on Contact)
◦ Use __r for custom objects. (For example, invoice statements are
linked to line items through the Line_Items__r relationship on
the Invoice_Statement__c custom object.)
for(account parent:
[SELECT Id, Name, Industry, AnnualRevenue,
( SELECT Name, Email, BirthDate FROM Contacts )
FROM Account WHERE Account.name like 'Grand
Hotels%']
)
{
//Since subquery is returning a list, if you want to check if child list is
empty.
if(!parent.contacts.isEmpty()){
for(contact child:parent.contacts){
system.debug('child :'+child.name);
}
}
else{
system.debug('no child');
}
}
for(list<account> parents: [SELECT Id, Name, Industry, AnnualRevenue,
( SELECT Name, Email, BirthDate FROM Contacts ) FROM
Account WHERE Account.name like 'Grand Hotels%']
)
{
for(account parent: parents)
{
//Since subquery is returning a list, if you want to check if child list is
empty.
if(!parent.contacts.isEmpty()){
for(contact child:parent.contacts){
system.debug('child :'+child.name);
}
}
else{
system.debug('no child');
}
}
}
Static Query
List<Account> aa = [SELECT Id, Name FROM
Account WHERE Name = 'Acme'];
Dynamic Query
List<Account> aa = database.query('SELECT
Id, Name FROM Account WHERE Name =
'Acme'');
Dynamic Query
◦ Creation of a SOQL/ SOSL string at runtime with an
Apex script.
◦ Enables you to create more flexible applications means
developer can pass parameters dynamically.
For example, you can create a search based on input from
an end user, or update records with varying field names on
different objects.
When to use Dynamic SOQL
◦ When you don't know until runtime which fields you
want to include in your query, or your WHERE clause
should be constructed differently based on something
that happened earlier in the code. You could even
query on different objects, depending on what's
needed.
Static Query (using select) .
//create a string
String s = 'mystring';
for(SObject o : obj){
Account a = (Account)o;
//do more here.....
}
Force.com SOQL and SOSL Reference
◦ https://developer.salesforce.com/docs/atlas.en-
us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql.ht
m