Standard formats of Relationship Queries in Salesforce
Standard Formats of Queries:-
Child obj name variable = “select child obj field1, parent obj name.parent obj field1, parent obj name.parent obj field2 from child obj name where condition”;System.debug (‘child obj records are’+variable);
System.debug (‘’ parent obj field1+variable .parent obj name.parent obj field1);
System.debug (‘parent obj field2+variable.parent obj name.parent objfield2);
Fetching single record form child - parent for standard objects:
List<Child obj name> variable = “select child obj field1, parent obj name.parent obj field1, parent obj name.parent obj field2 from child obj name where condition”;System.debug (‘child obj records are’+variable);
For (child obj name variable1: variable)
{
System.debug (‘’ parent obj field1+variable1 .parent obj name.parent obj field1);
System.debug (‘parent obj field2+variable1.parent obj name.parent objfield2);
}
Fetching Multiple record form child - parent for standard objects:
List<Child obj name> variable = “select child obj field1, parent obj name.parent obj field1, parent obj name.parent obj field2 from child obj name where condition”;System.debug (‘child obj records are’+variable);
For (child obj name variable1: variable)
{
Variable1.parent obj name.parent obj field1=’…………..’
Variable1.parent obj name.parent obj field2=’……………’
Update variable1.parent obj name;
System.debug (‘’ parent obj field1+variable1 .parent obj name.parent obj field1);
System.debug (‘parent obj field2+variable1.parent obj name.parent objfield2);
Updating Multiple record form child - parent for standard objects
List<Parent obj name> variable= “select fieldname1 (select field1, field2 from parent obj name.childs obj)from parent obj name where condition”;System.debug (‘parent obj records’+variable1);
For (parent obj name variable1: variable)
{
For (child obj name variable2:variable1.child plural obj name)
{
System.debug (‘child obj field1’+variable2);
System.debug (‘child obj field2’+variable2);
}
}
Enthusiastic about exploring the skill set of Salesforce? Then, have a look at the Salesforce Training Online to gather additional knowledge.
Fetching Multiple record form Parent - child for standard objects:
List<Parent obj name> variable= “select fieldname1 (select child obj field1, child obj field2 from parent obj name.child plural obj) from parent obj name where condition”;System.debug (‘parent obj records’+variable1);
For (parent obj name variable1: variable)
{
For (child obj name variable2:variable1.child plural obj name)
{
Variable2.child obj field1='……….';
Variable2.child obj field2='……….';
update variable2;
System.debug (‘child obj field1’+variable2);
System.debug (‘child obj field2’+variable2);
}
}
Updating Multiple record form Parent - child for standard objects:
list<parent obj API name> variable=“select parent obj field1 ,(select child obj field1,child obj field2from parent obj API name.child plural obj name__r) from parent object API name where condition);
System.debug (' parent object records'+variable); for (parent obj API name variable1:variable)
{
For(child obj singular name__c variable2:variable1.child plural obj name__r)
{
Variable2.field1 API name='………..'; Variable2.field2 API name='……….'; update variable2;
System.debug ('field1'+variable2);
System.debug ('field2'+variable2);
}
}
Updating Multiple record form Parent - child for custom objects:
list<child obj API name> variable=“select child obj field1,parent obj name __r.field1 from child obj APIname where condition”; system.debug ('child obj records are'+variable);
for( child obj API name variable1:variable)
{
Variable1.parent obj singular name__r.field1 API name='…….'; update variable1.parent obj singular name__r;
system.debug('parent obj records'+variable1.parent obj singular name__r.field1 API name);
}
For indepth understanding on Salesforce click on
- Salesforce Tutorials
- SalesForce Interview Questions & Answers
- Annotations in SalesForce
- Loops in SalesForce
- Apex Data Manipulation
- Creating Apex Classes
- Salesforce Lightning Training In Hyderabad