Question

I am working on a Trigger that uses many SOQL statements. I have tried following this guide http://wiki.developerforce.com/page/Best_Practice:_Avoid_SOQL_Queries_Inside_FOR_Loops and it doesn't work for me because IDs in Salesforce are not the exact same as the id's that come up for every item. Here's what I mean:

(Btw this is a child record calling a parent and so the parent child thing doesn't work for me)

list<opportunity> listOpportunity = [select id
                                     From opportunity
                                     Where id IN : *Trigger.oldmap.keyset()];*

for(Opportunity opportunity : listOpportunity)
{
     //Do Code here.

}

All I want is for this list to be populated with the correct opportunity(s) being triggered.

Was it helpful?

Solution

you do not need a SOQL query here, you can just use the following snippets

// for iterate trough new opportunities
for (Opportunity newOpp: Trigger.new) {
     // do something with new Opps
}
 // for iterate trough old opportunities
 for (Opportunity oldOpp: Trigger.oldMap.values()) { 
     // read old values on update trigger
 }

also you can combine it

 // for iterate trough old opportunities

for (Opportunity newOpp: Trigger.new) {
    if (newOpp.field1 != Trigger.oldMap.get(newOpp.Id).field1) {
        // do something for changed field
    }
}

==========================================================================

UPDATE

@user1991372

I hope this update will be helpful, here is the example of common approach for solving such issue with SOQL in a loop

//------------------ Trigger

Map<Id, List<Account>> accountsByOpp = new Map<Id, List<Account>>();
 List<Account> accounts = [SELECT Oppty
                                 ,name 
                           FROM account 
                           WHERE oppty IN Trigger.newMap.keySet()];
accountsByOpp = splitListByKey(accounts, 'oppty');

for (Opportunity opp: Trigger.old) {
    List<Account> accs = accountsByOpp.get(opp.Id);
    if (accs != null) {
        // do something
    }
}

//------------------- Utility apex

class CommonException extends Exception {}

public static Map<Id, List<sObject>> splitListByKey(List<sObject> sourceList, String key) {
    if (sourceList == null) {
        throw new CommonException('ERROR: splitListByKey(sourceList, key) got incorrect first parameter.');
    }
    if (key == null || key == '') {
        throw new CommonException('ERROR: splitListByKey(sourceList, key) got incorrect second parameter.');
    }
    Map<Id,List<sObject>> result = new Map<Id,List<sObject>>();
    List<sObject> tmpObjs;
    for (sObject obj: sourceList) {
        tmpObjs = new List<sObject>();
        if (obj.get(key) != null && result.containsKey((Id)obj.get(key))) {
            tmpObjs = result.get((Id)obj.get(key));
            tmpObjs.add(obj);
            result.put((Id)obj.get(key), tmpObjs);
        } else if (obj.get(key) != null) {
            tmpObjs.add(obj);
            result.put((Id)obj.get(key), tmpObjs);
        }
    }
    return result;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top