You need to sign in to do that
Don't have an account?

Trigger - multiple iterations when inserting multiple line items at once to opportunitylineitems
I have written a trigger to update the amount and quantity on the opportunitylineitems table.
It works fine if I am insertingonly one product to the opportunitylineitem. However, if I am writting 2 or more products (line items) it runs through tthe trigger multiple times and I end up with incorrect data.
Any help would be appreciated!!
thanks,
Karen
trigger opplineitemstest on OpportunityLineItem (before insert, before update){
Set<Id> OrderLineIds= new Set<Id>();
Set<Id> LineItemIds= new Set<Id>();
Set<String> PBEIdSet = new Set<String>();
Set<String> Prod2IdSet = new Set<String>();
for (OpportunityLineItem insertedlineitem : system.Trigger.new)
{
System.debug('INSIDE OPP FOR LOOP!!!');
if (insertedlineitem.PricebookEntryId != null) {
System.debug(insertedlineitem.PricebookEntryId );
PBEIdSet.add(insertedlineitem.PricebookEntryId );
}
List<PricebookEntry> ParentPricebookEntry = [SELECT Product2Id
from PricebookEntry
where id in :PBEIdSet];
for(PricebookEntry pbes: ParentPricebookEntry) {
if (pbes.Product2Id != null) {
System.debug('PROD ID ' + pbes.Product2Id);
Prod2IdSet.add(pbes.Product2Id);
}
List<Product2> ParentProd = [SELECT ProductCode
from Product2
where id in :Prod2IdSet];
for(Product2 prods: ParentProd) {
System.debug('PROD CODE ' + prods.ProductCode);
if(prods.ProductCode == 'AMS300' || prods.ProductCode == 'AMS305' || prods.ProductCode == 'AMS310-5PK' || prods.ProductCode == 'AMS350-5PK' || prods.ProductCode == 'AMS505' || prods.ProductCode == 'MDS405') {
System.debug('BEFORE QTY ' + insertedlineitem.quantity);
insertedlineitem.quantity = insertedlineitem.quantity * 5;
insertedlineitem.unitprice = insertedlineitem.unitprice / 5;
System.debug('AFTER QTY ' + insertedlineitem.quantity);
}
if(prods.ProductCode == 'AMS300-10PK' || prods.ProductCode == 'AMS305-10PK' || prods.ProductCode == 'AMS600' || prods.ProductCode == 'AMS604' || prods.ProductCode == 'AMS607' || prods.ProductCode == 'AMS612'|| prods.ProductCode == 'AMS700') {
insertedlineitem.quantity = insertedlineitem.quantity * 10;
insertedlineitem.unitprice = insertedlineitem.unitprice / 10;
}
System.debug('77');
}
}
system.debug('END OF OPPLINEITEM LOOP');
}
system.debug('end!!!!!');
}
The problem is that you are initializing the sets outside of the loop and then adding to them on every iteration of the loop. For item 1 the sets will have the IDs from item 1, for item 2 they will have the IDs for item 1 and 2, and so on.
You could move the intiialization of the sets into the for loop, however you will hit limits on the number of SOQL queries that can be run. I recommend refactoring a bit.
In pseudo code:
This will allow you to handle more than one item, and also limit your SOQL queries down to 2, instead of 2 * [number of line items].
Regards,
Mark
Thanks for your help!!
Just for a quick test I moved the initializing inside the for loop. When there are multiple lines it processes the first line item once - (correctly), the second line item 2X (values are multiplied and divided by 5 - one too many times) and the third line item 3X - (values are multiplied and divided by 5 - two too many times)
I wanted to test this before I start refactoring.
I have only written a few apex triggers so your experience is vary helpful!
Thanks again!
Karen
Hi Karen,
After reading through your code again, I can see that each line item will only have one PriceBookEntry, and each PriceBookEntry will only have one Product2. This means there is no need for Sets in your "non-bulkified" trigger. So here is a cut at the non-bulkified version:
And here is a cut at a bulkified version:
One problem still remaining with both of these solutions is that whenever you add new 5x or 10x product codes, you will have to update the trigger. You could get around that by adding an integer field directly on the Product2 object. The field could store the quantity multiplier for that Product2, and that multiplier could be used by the trigger. On the other hand, if you don't anticipate alot of product changes, having the codes hard-coded in the trigger might be fine for now.
Regards,
Mark
Hi karen,
As per my understanding i have done the coding, avoiding queries inside the loop.
Hope it help and would be as per your requirement.
I dint understand the req. properly, was not able to test the code for you.
Trigger:
Hope it helps, and Let me know if you face any issue's