You need to sign in to do that
Don't have an account?
Sunay
Need help on Bulk Trigger to add Opportunity Line Items
Hi I have written a Apex trigger for inserting opportunity line items and it is working fine. But the only issue is I am not able to insert more than 15 records in one shot. Please Help me!!
The code is as below:
trigger OppportunityProductInsert on Opportunity (after Insert, after Update ) {
Integer Num =1;
For (Integer I = 0; I < Num; I++)
For (Opportunity a : Trigger.new)
if (a.RecordTypeId=='01290000000UUr2' && (a.Number_of_Resources__c-a.Total_Number_of_Opportunity_Products__c)>0)
{
Product2 Prod = [select Id from Product2 limit 1 ];
Pricebook2 PB = [select Id from Pricebook2 where IsActive=True limit 1 ];
PricebookEntry P = [select Id from PricebookEntry where CurrencyIsoCode=:a.CurrencyIsoCode And Pricebook2Id=:PB.Id limit 1 ];
OpportunityLineItem OppLI = new OpportunityLineItem (OpportunityId=a.id, UnitPrice=0, Quantity =1, PricebookEntryId=P.Id);
Insert OppLI;
}
}
You need to bulkify your trigger
here is a link to a good article on the topic: Writing Bulk Triggers for Salesforce.com
Thanks for the Link. But I could not find a solutions out of it even after trying for several times. If you can help me through the code where exactly the changes has to happen, that might help me in solving this issue of writing bulk trigger.
Since I am new to coding and development, I need more help towards this.
Regards,
Sunay
You've got two problems that I see. The insert statment within the For loop and the three SOQL queries in the For loop. As a general rule, you should never have DML operations or SOQL queries inside of a loop. I also don't see you using the Product that you retrieve with the first query.
To bulkify, you need to move your queries and DML statement outside the loop. Typically you would do this by collecting the data you need to query on in a list or set, then performing your query, then loop through the appropriate records again to do something to them and add to another list that you'll use for the DML statement.
I'm not sure what you're really trying to accomplish with the queries within you loop, so I won't try to rewrite it for you. It should be a pretty simple one to write, but I'm confused by those queries....
Hi Sunay,
You have not optimized your trigger to save limits.Using dml statement (Insert OppLI; in your case) and SOQL in LOOP is not the right way of writing triggers.
You should write your triggerd using collections like this
trigger OppportunityProductInsert on Opportunity (after Insert, after Update ) {
Hi,
Thanks for the reply!!
But i tried using the same code and i got this error below:
Error:Apex trigger OppportunityProductInsert caused an unexpected exception, contact your administrator: OppportunityProductInsert: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OppportunityProductInsert: maximum trigger depth exceeded Opportunity trigger event AfterUpdate for [006O00000025BHf] Opportunity trigger event AfterUpdate for [006O00000025BHf] Opportunity trigger event AfterUpdate for [006O00000025BHf] Opportunity trigger event AfterUpdate for [006O00000025BHf] Opportunity trigger event AfterUpdate for [006O00000025BHf] Opportunity trigger event AfterUpdate for [006O00000025BHf] Opportunity trigger event AfterUpdate for [006O00000025BHf] Opportunity trigger event AfterUpdate for [006O00000025BHf] Opportunity trigger event AfterUpdate for [006O00000025BHf] Opportunity trigger event AfterUpdate for [006O00000025BHf] Opportunity trigger event AfterUpdate for [006O00000025BHf] Opportunity trigger event AfterUpdate for [006O00000025BHf] Opportunity trigger event AfterUpdate for [006O00000025BHf] Opportunity trigger event AfterUpdate for [006O00000025BHf] Opportunity trigger event AfterUpdate for [006O00000025BHf] Opportunity trigger event AfterUpdate for [006O00000025BHf]: []: Trigger.OppportunityProductInsert: line 17, column 1
Regards,
Sunay
Hi David,
Thanks for your response!!
Let me explain to you about the trigger in detail:
When a new opportunity is created for a record type 'T&M', we will be entering the number of resources required in a field called 'Number of Resources'. When the record is saved, based on the number of resources, the opportunity line items should get inserted automatically which is editable.
Also when the opportunity record is edited or updated, the line items should get inserted based on the number of resources.
This is what we are trying to achieve, and we were successful with the above code for a limit of 15 resources. Suppose I give as '16 or more' while inserting or add '16 or more ' while updating, the above mentioned error shows up.
I hope this can help you in understanding the requirement and will be able to help me on this.
Regards,
Sunay
Hi,
Can anybody help me on the above as it is quite urgent?
Would appreciate your help on this!!!!
Regards,
Sunay
Do you hae any trigger on OpportunityLineItem or any field update using workflow rule.
Hi,
Thanks for the reply!!
Ya we have triggers on Opportunity Line item to update another custom object called "Forecasts". Also we have workflow rules on the Opportunity Line item.
Regards,
Sunay
Here is your problem one trigger of yours calls another then I am sure you are going deeper in triggers calling trigger. You need to use static variable in this case to stop excecution of some triggers.
Please check this : http://forceschool.blogspot.com/2011/05/writing-apex-trigger-issues-and_24.html
Even though your case is not of bulk upload as given in example ut you can use similar approach by using static variable.
Set this static variable to false before you update opportunityLineItem records in this trigger.
Hi,
Since I am new to coding. Can you please help me on the above code which is posted. I am confused on the coding part.
Regards,
Sunay
Create a class
In your trigger in OppLine item
In your trigger
I hope it will work for you.
Hi Shashikant,
I tried the above, but whenever I insert or update, only one record is getting created. How do I create more number of records???
Please help!!
Regards,
Sunay
I did not get your question, because how many number of OppLineItem gets created depends on your trigger which seems to me is not correct. I have provided some acrticles on Apex Trigger including bulk trigger handlin please check them.
http://forceschool.blogspot.com/search/label/Apex%20Triggers
Hi,
Let me explain to you about the trigger in detail:
When a new opportunity is created for a record type 'T&M', we will be entering the number of resources required in a field called 'Number of Resources'. When the record is saved, based on the number of resources, the opportunity line items should get inserted automatically which is editable.
Also when the opportunity record is edited or updated, the line items should get inserted based on the number of resources.
This is what we are trying to achieve, and we were successful with the above code for a limit of 15 resources. Suppose I give as '16 or more' while inserting or add '16 or more ' while updating, the above mentioned error shows up.
I hope this can help you in understanding the requirement and will be able to help me on this.
Regards,
Sunay