3

I'm trying to bulkify a trigger, previously when I've done this there has been a relationship between the objects within the trigger and i've been able to figure it out from other peoples triggers. This one looks to the Custom Settings, so there is no relation between the opp and the data within custom settings. Can anyone help?

The basic idea is to populate a lookup field on a new Opportunity with the correct user from a list in custom settings.

Here is the unbulkified trigger:

       for (Opportunity newOpp : Trigger.new) { 

           // Query custom settings for matching region and div
            String area= newOpp.area__c;
            String code= newOpp.code__c;               
            String c = [select user__c from delivery__c where area__c = :area AND code__C = :code].user__c;

           // Update Opporutnity with new value from custom settings
           if (c != null){
                    newOpp.userid__c = c;
                    }                          
}

This works fine but a bulk insert of Opportunities would hit the limits. then loop through all opps in the trigger, updating the lookup field with the user id. However as there's no relation, I dont know how to match them up.

Any ideas?!

Thanks in advance!

  • possible duplicate of General trigger bulkification - best practices – Christian Deckert Aug 21 '14 at 18:20
  • @ChristianDeckert - No need to flag to close old answered posts based on the answers to your new post. If you want to add a link to your post as additional information that is great but please do not flag them for closure – Eric Aug 21 '14 at 19:37
4

You have the right idea but need to think about this slightly different.

First step, you get 2 sets of all of the different areas and codes. Just like you normally would for a Set of Ids for something related.

After you query for that you loop through all of those and place them into a map. This is where it gets different. The key to the Map should be the area and code combination. So:

for (delivery__c sl: [SELECT User__c, area__c, code__c FROM delivery__c WHERE area__c IN :area AND code__c IN :code])
{
  dlMap.put(dl.area__c + dl.code__c, dl);
}

Now, you would do the exact same thing when you are looping through your Opportunities again but use that combination to grab the associated delivery__c list.

EDIT:

First loop:

Set<String> area = new Set<String>();
Set<String> code = new Set<String>();
for (Opportunity opp: Trigger.new)
{
  area.add(opp.area__c);
  code.add(opp.code__c);
}

My query already shows how to include the variables in there.

There will be a total of 3 loops.

1) Loop through Opps to get area/code.

2) Loop through delivery__c to put together the map.

3) Loop through Opps again to do your work.

  • Hi dphil, thanks for much for taking the time to help. What you have written makes sense, however I'm still a little confused. How is it possible to include variables in the SOQL query? Surely those variables would need to be in the for loop ...which would count towards limits each time. for (Opportunity newOpp : Trigger.new) { String revinue String division .... – Jay Jul 3 '14 at 16:11
  • Edited my answer to show you. – dphil Jul 3 '14 at 16:34
  • Ah I see, sorry I misunderstood. That's awesome, I'm just correcting my trigger now. Thanks so much for explaining. Have a great weekend. – Jay Jul 3 '14 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.