Here's how I'd recommend querying the objects where possible, assuming you want all of them and have the relationships between the various levels of children and the parent.
map<Id,ObectA__c>ParntMapA = new map<Id,ObjectA__c>([SELECT Id, Field1, Field2...(SELECT ID, Field1__c, Field2__c FROM ObjectB__r), (SELECT ID, Field1__c, Field2__c FROM ObjectC__r), (SELECT ID, Field1__c, Field2__c FROM ObjectD__r), (SELECT ID, Field1__c, Field2__c FROM ObjectE__r) FROM ObjectA__c WHERE ID =: Some relationship with Other Objects]);
Once you have that, you can easily retrieve and manipulate the data using for loops like the following:
set<Id>ObAIds = new set<Id>;
ObAIds = ParntMapA.keyset();
list<ObjectA__c>LstA = new list<ObjectA__c>();
list<ObjectB__c>LstB = new list<ObjectB__c>();
list<ObjectC__c>LstC = new list<ObjectC__c>();
list<ObjectD__c>LstD = new list<ObjectD__c>();
list<ObjectE__c>LstE = new list<ObjectE__c>();
list<ObjectF__c>LstF = new list<ObjectF__c>();
for(Id aID:ObAIds){
ObjectA__c obA = ParntMapA.get(aID);
LstB = obA.ObjectB__r;
LstC = obA.ObjectC__r;
LstD = obA.ObjectD__r;
LstE = obA.ObjectE__r;
for(ObjectB__C obB:LstB){
obB.Id = Id of each unique record returned for the child;
obB.RefobAID__c = aID; // (or obA.Id)
}
for(ObjectC__C obC:LstC){
obC.Id = Id of each unique record returned for the child;
obC.RefobAID__c = aID; // (or obA.Id)
}
for(ObjectD__C obD:LstD){
obD.Id = Id of each unique record returned for the child;
obD.RefobAID__c = aID; // (or obA.Id)
for(ObjectF__c obF:LstF){
obF.Id = Id of each unique record returned for the child;
obF.RefobAID__c = aID;
obF.RefobDID__c = obD.Id; // (or obD.Id)
}
}
for(ObjectE__C obE:LstE){
obE.Id = Id of each unique record returned for the child;
obE.RefobAID__c = aID; // (or obA.Id)
}
} // end of for loop over parent ObjectA__c
In this manner, you can retrieve everything you need for each object, make revisions as your business model requires, then perform updates on each object type afterward or do additional calculations as necessary from the lists you've generated.
Edit:
You'll notice the addition of ObjectF
to my original code above where I've nested it inside ObjectD
. I've also added references to from each of the child Objects to ObjectA
as well as a reference from ObjectF
to ObjectD
. Its the use of these relationships that will help you gather the Id's
you'll need to construct your main query for your triggers.
Once you have those, you can then utilize the kind of code that @Keith C
has kindly posted. I didn't want to take you there until you understood the relationships between the children, how to construct the query, and how to pull the from the map that gets returned so you can use the results. Until you get this part, there's not much you'll be able to do with Keith's excellent code and advice. These sub-loops are important because they can return many results for a single ID of ObjectA
, or for that matter, of say ObjectF
relative to ObjectD
.
I hope we'll see you posting your code once you begin working on this in earnest.