The children records will be contained within standard List collections under the parent object record.
Enhancing your much-too-genericized code sample:
Map<Id, Account> AcctOppCustomObjectMap = new Map<Id, Account>([SELECT Id
,(SELECT xxx
FROM xxx__r)
,(SELECT yyy
FROM yyy__r)
FROM Account
WHERE Id IN :idSet]);
List<yyy__c> yThingsToUpdate = new List<yyy__c>();
for (Id accountId : AcctOppCustomObjectMap.keyset()) {
Account acct = AcctOppCustomObjectMap.get(accountId);
// the acct reference will have child lists for your subqueries
// they will not be null, but they could be empty lists
List<xxx__c> xxxList = acct.xxx__r;
List<yyy__c> yyyList = acct.yyy__r;
// iteration of the child records possible now using these two lists
for (xxx__c xThing : xxxList) {
for (yyy__c yThing : yyyList) {
if (xThing.value == yThing.value) {
// create your relationship and
// add to a list for bulk update / insert
yThingsToUpdate.add(new yyy__c(Id = yThing.Id
, xxx__c = xThing.Id));
break; // exit the for loop on the first match if desired
}
}
}
}
if (!yThingsToUpdate.isEmpty()) {
update yThingsToUpdate;
}
I would probably iterate each child list just once each to create maps of the contents by a desired key so that I could perform a Map.get() instead of repeatedly iterating lists to find the other child record to compare against. Your code sample needs more specific information in it.
Repeated iteration of the yyy list within the xxx list will limit your ability to process records since these will use script statements to iterate and match the records.
UPDATE
If you want to group CaseComment
records by Case.AccountId
, the following makes more sense:
Map<Id, List<CaseComment>> accountIdToComments = new Map<Id, List<CaseComment>>();
for (CaseComment comment : [
SELECT Parent.AccountId, Other_Fields__c FROM CaseComment
WHERE ...
]){
Id accountId = comment.Case.AccountId;
if (!accountIdToComments.containsKey(accountId))
accountIdToComments.put(accountId, new List<CaseComment>());
accountIdToComments.get(accountId).add(comment);
}
You've added the ParentId
to your join on CaseComment
, but not to your query on Case
itself.
List<Case> records = [
SELECT (SELECT ParentId, ... FROM CaseComments), ParentId, ...
// ^ not on Case object ^ on Case Object
FROM Case WHERE ...
];
Note you've confused yourself a bit by naming the collection comments
. The collection returned by the query is not a List<CaseComment>
. It's a List<Case>
.
If you just want to be able to get the List<CaseComment>
for a given Case
Id, you don't need any for
loop at all:
Map<Id, Case> records = new Map<Id, Case>([/*query*/]);
// intermediate code ...
Case parentCase = records.get(someCaseId);
if (parentCase != null)
{
List<CaseComment> comments = parentCase.CaseComments;
// additional processing
}
If you're actually trying to group the Case
records themselves by their ParentId
, then you do indeed need to add that field to your top level SELECT
clause:
Map<Id, List<Case>> groupedCases = new Map<Id, List<Case>>();
for (Case record : [/*above query*/])
{
if (!groupedCases.containsKey(record.ParentId))
groupedCases.put(record.ParentId, new List<Case>());
groupedCases.get(record.ParentId).add(record);
}
Best Answer
Yes, there is.
You can use the
containsKey()
method of the map class.There's even a way that you can avoid having any explicit loop at all! (I say "explicit" loop, because the back-end implementation of at least some of the collection class methods probably involves a loop of its own)