[SalesForce] get / compare children values on map

For a trigger on Opportunity:

Objects are Account, Opportunity, and a Custom Object. There is one account to many opportunities and one account to many custom objects.

I need to compare values on Opportunity and Custom Object, and create a lookup from Opportunity to Custom Object if a few conditions are true. Using loops w/o any Maps I built working code but it cannot handle bulk inserts.

Is building a Map on Account the correct approach?

>  map  AcctOppCustomObjectMap = new map ([Select ID, (Select xxx From xxx__r),(Select yyy From yyy__r) From Account where Id IN :idSet]);

How do I iterate over children records? I know how to get the first values from a child record:

string strName1 = AcctOppCustomObjectMap.get(i).xxx_r[0].Name;

A couple of links I found helpful: one, two.

Thanks!

Best Answer

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.

Related Topic