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.
To access a Map (as in oldMap), you will need to access by Id. If it is just the old Opportunity Owner ID you are after, you can simply access it as - no need to perform the query:
trigger TestTrigger on Opportunity (before update) {
for(Opportunity opp : Trigger.new){
Opportunity oldOpportunity = Trigger.oldMap.get(opp.ID);
oppOwnerId = oldOpportunity.OwnerId;
...
}
}
However, i suspect you are not and that you do need to perform the query to retrieve some other information, in which case you are thinking about this the wrong way. You need to 'bulkify' your Trigger.
What you will have to do instead of what you are suggesting is refactor your trigger a little. You should first collect a set of OwnerIds from the 'old' collection, for example:
Set<Id> ownerIds = new Set<Id>();
for( Opportunity opp : Trigger.old ) {
ownerIds.add( opp.OwnerId );
}
Then, using that set, you should query all of the Users outside of the loop (i.e. once for all the OwnerIds) and create a Map for easy access, for example:
Map<Id,User> userMap = new Map<Id,User> ( [ Select Id From user Where Id IN ownerIds ] );
Then, from within the Loop, you can access the User as you wish using the map:
for( Opportunity opp : Trigger.new ) {
User oppOwnerId = userMap.get( Trigger.oldMap.get( opp.Id ).OwnerId );
}
This is a rather futile example because the code is only returning the User Id which you essentially have already anyway (in the oldMap).
Best Answer
You can add this sort of guard in your trigger:
so that the queries are only done if the checkbox is checked and when the checkbox value has changed (if that is appropriate too).