function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ethanoneethanone 

need bulk help

As a newbie, I'm struggling to refactor this trigger code to work in bulk (works fine in a non-bulk test case). The trigger is supposed to update the AccountId field on the opportunity object based on information from a related field. 

 

 

trigger UpdateOppAccountName on Opportunity (before update) {

for(Opportunity o : trigger.new){

Account_Property_Link__c oldPIDOwner = [SELECT Account__c FROM Account_Property_Link__c WHERE Property__c = :opp.Preceding_Property__c];

opp.AccountId = oldPIDOwner.Account__c;

}

I think I need to have a list of Opportunities updated with a list of Account_Property_Link__c objects, but I just can't seem to get the syntax right.  Can someone give me a hand? 

 

Thanks in advance.

 

 

SFDCDev7SFDCDev7

Hi,

 

Check the below link . It will help you.

http://sfdc.arrowpointe.com/2008/09/13/bulkifying-a-trigger-an-example/

 

-AJ

jeffdonthemic2jeffdonthemic2

You can also try: Writing Bulk Triggers For Salesforce.com

 

Jeff Douglas

Appirio, Inc.

http://blog.salesforce.com 

jeffdonthemic2jeffdonthemic2

You can also try: Writing Bulk Triggers For Salesforce.com

 

Jeff Douglas

Appirio, Inc.

http://blog.jeffdouglas.com 

ethanoneethanone

Thanks for the posts.  I think my problem might be more with a fundamental understanding of how a map collection works.  In the post's example here:

 

trigger AddOwnerColor on Account (before insert, before update) {

// create a set of all the unique ownerIds

Set<Id> ownerIds = new Set<Id>();

for (Account a : Trigger.new)

ownerIds.add(a.OwnerId);

 

// query for all the User records for the unique userIds in the records

// create a map for a lookup / hash table for the user info

Map<Id, User> owners = new Map<Id, User>([Select Favorite_Color__c from User Where Id in :ownerIds]);

 

// iterate over the list of records being processed in the trigger and

// set the color before being inserted or updated

for (Account a : Trigger.new)

a.Owner_Favorite_Color__c = owners.get(a.OwnerId).Favorite_Color__c;

}

The owners map should contain the Id of the user object and the user object itself, right?  So, the next for loop doesn't make sense to me.  Since the owner map doesn't contain the  account ID, how can you use that as an index in owners.get(a.OwnerId)?  This only seems to work if the account id is the same as the user id (which may be the case in this example, but not in my case).

 

I think I need a map collection that contains the Opportunity ID and the associated Account_Property_Link__c object.  How can I get that?

 

sfdcfoxsfdcfox

trigger UpdateOppAccountName on Opportunity (before update) { // Set of ID values for preceding properties Set<ID> precedingProperties = new Set<Id>(); // Map of the Property to the account. Map<String,Account_Property_Link__c> acctProps = new Map<String,Account_Property_Link__c>(); //Accumulate all the preceding property entry ids. for(Opportunity O:Trigger.new) precedingProperties.add(o.Preceding_Property__c); // Query all of the Property ids for the related information, // and store this data in a map. for(Account_Property_Link__C acctPropLink: [select id, account__c, property__c from account_property_link__c where property__c in :o.preceding_property__c]) acctProps.put(acctPropLink.property__c,acctPropLink); // Use the map we just built to assign the account id // values back into the opportunity. for(Opportunity O:Trigger.new) o.AccountId = acctProps.get(o.preceding_property__c).account__c; }

This sort of "three-pass" method is very common when bulkifying a trigger. You need one pass to get all of the lookup values you'll be using, a second pass to query the related data, and a third pass to use the lookup from the second pass to assign values. I hope my example clarifies this rather typical use of Map objects.