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
lalitha_sfdclalitha_sfdc 

How can I get the list of values of child 1 from child 2 which are associated to the same parent

I have list of records from child 1 from the below query
child1list=[select name,id,parent__c,field11,field12 from child1__c]

I have list of child2
child2list=[select id,name,parent__r,parent__r.name,field21,field22 from child2 where parent__c in : parentids]

But I Have to update child1 fields in for loop as below accessing from child2 through ​ parent
for(child1__c child1 : child1list){
child1list.field11=child2list[].field21;//how to associate the related record here in child2list[] 
}

Child2 has one to one mapping with parent. 
 
Best Answer chosen by lalitha_sfdc
lalitha_sfdclalitha_sfdc
In the above requirement I have to process and update the fields of child1 from the certain fields of child2 which are children of common parent. And here child1 can have any no of records but the child2  has one to one mapping with parent. (ex for each parent there is only one child 2).

I met the above requirement using map of (parentTochild2).
 
Map<Id,Child2> parentIdToChild2Record = new Map<Id,Child2>();
for(child2 c2 : [select id,name,parent__r,parent__r.name,field21,field22 from child2 where parent__c in : parentids]){
   // since we know that each parent will only have 1 child2, there is no need to verify if the map already contains a value for that parentId
   parentIdToChild2Record.put(c2.parent__c,c2);
}

Now we can fetch the values as below.
 
for(child1__c child1 : child1list){
   child1list.field11 = parentIdToChild2Record.get(child1.parent__c).field21;
}

 

All Answers

EldonEldon
Please elaborate your req.
What if child1 have 3 records and child2 have 5 records> How do you want to handle this situation?
 
lalitha_sfdclalitha_sfdc
In the above requirement I have to process and update the fields of child1 from the certain fields of child2 which are children of common parent. And here child1 can have any no of records but the child2  has one to one mapping with parent. (ex for each parent there is only one child 2).

I met the above requirement using map of (parentTochild2).
 
Map<Id,Child2> parentIdToChild2Record = new Map<Id,Child2>();
for(child2 c2 : [select id,name,parent__r,parent__r.name,field21,field22 from child2 where parent__c in : parentids]){
   // since we know that each parent will only have 1 child2, there is no need to verify if the map already contains a value for that parentId
   parentIdToChild2Record.put(c2.parent__c,c2);
}

Now we can fetch the values as below.
 
for(child1__c child1 : child1list){
   child1list.field11 = parentIdToChild2Record.get(child1.parent__c).field21;
}

 
This was selected as the best answer
EldonEldon
You can do it in a simple way like below,
 
child2__c c2 = [select name,field21 from child2__c where parent__c in :parentids]; //assuming only one child2

child1__c c1 = [select name,field11 from child1__c where parent__c in :parentids];

for(child1__c c : c1){
    c.field11=c2.field21;
}
update c1;

Let me know your thoughts

Regards
lalitha_sfdclalitha_sfdc
If we query child2 list and child1 list with in the parentids list we never know whether both refering the same parentids. In order to match the same parentids for both children we use a map as I mentioned in the above code. just refer that mark as best If u find it useful.

Regards
EldonEldon
For that we save the parent id in the variable 'parentids'. And use it in the query. Then we will get the children with same parent from both child1 and child2. Did you try the code i gave above?