You need to sign in to do that
Don't have an account?
srikanth11
help for trigger soql limit
hi all
i have written this trigger but in sandbox its working but in live its giving errors plz some one help me rewr
public class updateasset1 { public static boolean isfuture=false; public static void oli (OpportunityLineItem[] OLI) { if (isfuture==false){ isfuture=true; // for(OpportunityLineItem ol: OLI){ OpportunityLineItem ol = OLI[0]; ol = [Select UnitPrice,OpportunityId, Opportunity.Account.Id , Quantity,Fully_Shipped_Date__c,Shipped_Quantity__c, PricebookEntry.Product2Id,Create_Asset__c, PricebookEntry.Product2.Name,Product_Shipment_Status__c, Description From OpportunityLineItem where OpportunityLineItem.Id = :ol.Id limit 1]; Asset[] ast = new Asset[]{}; Asset a = new Asset(); if(ol.Product_Shipment_Status__c == 'Fully Shipped'&& ol.Create_Asset__c == false){ a = new Asset(); a.Name = ol.PricebookEntry.Product2.Name; a.Asset_Location__c = 'NONE' ; a.AccountId = ol.Opportunity.Account.Id; a.Product2Id = ol.PricebookEntry.Product2Id; a.PurchaseDate =ol.Fully_Shipped_Date__c; a.Quantity = ol.Quantity; a.Price = ol.UnitPrice; ast.add(a); ol.Create_Asset__c = true; update OL; } upsert ast; } //} } }
trigger updateasset1 on OpportunityLineItem (after update) {OpportunityLineItem[] OLI = Trigger.new; updateasset1.oli(OLI); }
plz help me resolve the issue
ite the code to avoid the errors
You are running a database query within a For loop.
What you need to do is to store the IDs in a ID set and then run another query outside of the For loop to lookup ids in the ID set.
I think you commented for loop as it was causing you issue , if so then uncooment and chnage that part of code like this
your answer helped me shasikanth sharma . sir my trigger is calling other triggers and the soql query limit is increasing may be its occuring when upserting opportunity line items so how can i maake my trigger so that it does not call other triggers
you can use a static variable for this use that as in condition over all the triggers that you do't want to get executed.
see this example in my blog : http://forceschool.blogspot.com/2011/05/writing-apex-trigger-issues-and_24.html
let me know if any issue in implementing this.
sir i read u r blog its very usefull i rewrite my code using u r help but now at what part should i we set the static variable false
In that example thre were three steps
1)Creating a Class with static variable
2)Adding that to trigger
3)Setting the value to false in class
So as you finished first two so now you just need to put it like where you are inserting opportunity line item as you have a trigger on this object
Constants.runTrigger = false;
update OL;
sir my test case i am writing for this is giving dml exceprtion plz help me with it
Check the validation rule , you must be having one which verifies status, do you set status in the trigger that you have blocked using static variable? Please let me knwo in which line you are getting and on which object.
IN case you have trouble in tracing please share your final trigger code both that you blocked and this one in which you have set sataci variable to false.
sir i am getting errors in setting the static variable to false i think i have written the class and giving it to trigger in wrong way plz help me with it sir