You need to sign in to do that
Don't have an account?
Trying to update multiple fields on OpportunityLineItem record from Product2
trigger oppLineItem_prod_data_trigger2 on OpportunityLineItem (before insert, before update) { Set<String> opportunityLineItemIDs = new Set<String>(); //We create a list for all opportunityLineItem records we will ultimately update to //enable us to perfoirm a single bulk update List<OpportunityLineItem> updtOpportunityLineItems = new List<OpportunityLineItem>(); //We loop through the updated records to get the Id, and add it to the set for(OpportunityLineItem oli : trigger.new) { if(!opportunityLineItemIDs.contains(oli.Id)) { opportunityLineItemIDs.add(oli.Id); } //This is our holding list for opportunityLineItem and the associated Product2 attributes OpportunityLineItem[] opportunityLineItems = new List<OpportunityLineItem>(); //We can now add the product2 attributes to the opportunityLineItems list created right above opportunityLineItems = [SELECT pricebookentry.product2.sbu_abbr__c, pricebookentry.product2.bu_abbr__c FROM OpportunityLineItem ]; //Old code //[Select Id, product2.sbu_abbr__c, product2.bu_abbr__c //From PricebookEntry //where id in '01u30000000M4EcAAK']; Map<Id, OpportunityLineItem []> oliProdAttributes = new Map<Id, OpportunityLineItem[]>(); //Old code //Place the data from the query into the map // Now use the map to set the appropriate attribute on every OpportunityLineItem processed by the trigger. //for (OpportunityLineItem oli : Trigger.new){ // oli.bu_abbr__c = entries.get(oli.pricebookEntryId).product2.bu_abbr__c; // oli.sbu_abbr__c = entries.get(oli.pricebookEntryId).product2.sbu_abbr__c; } for (OpportunityLineItem o :opportunityLineItems) { oliProdAttributes.put(o.Id, o.sbu_abbr__c, o.bu_abbr__c); } //Once we have updated the opportunityLineItem based on the data in Product2, we add it to //the opportunityLineItems list we created at the beginning updtOpportunityLineItems.add(oli); } //Once the account for loop has finished, we execute once single update of OpportunityLineItem update updtOpportunityLineItems; }
Hum... I didn't think anybody else was doing this... copying fields from Product2 to OpporutnityItem... OpportunityLineItem.PriceBookEntry.Product2.TheField__c seemed good enought to me... anyway,
If this is after insert/update,
OpportunityLineItem[] oliList = [select sbu_abbr__c, bu_abbr__c, pricebookentry.product2.sbu_abbr__c, pricebookentry.product2.bu_abbr__c FROM OpportunityLineItem where id in :opportunityLineItemIDs];
oli.bu_abbr__c = oli.pricebookentry.product2.bu_abbr__c;for(OpportunityLineItem oli : oliList) {
oli.sbu_addr__c = oli.pricebookentry.product2.sbu_abbr__c;
}
update oliList;
is enought. If it's before insert/update, you want to put the value in the Trigger.new[i] and you want to avoid governor limit, then you just cache PriceBookEntry Objects (Trigger.new doesn't have related object infromation (just the foreign key), so I don't think you can get PriceBookEntry.Product2Id without your own query).
Is there such a thing as "multiple dimension Map"? I wish there could be...
ThomasTT