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
ShirinShirin 

apex trigger

Hi,

I am new to apex coding & trigger. We have an opportunity and opportunity products record. There is a field called number of resources. When a user enters the number of resources ( for example: 5) the same number of opportunity products should be created (for example:5). I have written a trigger as below: but i can only enter 15 at a time. If the number of resources goes beyond 15, an error occurs. Can anybody help me on this?

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;
}
}
HaagenHaagen

Hi Shrin,

 

you should look at batching your trigger. 

 

1. First move the soql-queries outside the for-loops. Otherwise your soql-limit will be out in just a few runs.

2. You should insert the OpportunityLineItem's one-by-one, because this will also eat up your limits. Instead create a list of OpportunityLineItems and add the new ones to the list. In the end of your trigger you could check if the List has elements, if so, you can insert the whole list at once. 

 

Have a look at this link:

http://blog.jeffdouglas.com/2009/04/20/writing-bulk-triggers-for-salesforce/

 

Cheers!

 

Martin

Rahul S.ax961Rahul S.ax961

Hi Shirin,

Modified your code to avoid errors.

Hope it help :)

----------------------------------------------------------------------------------------------------------------------------------

trigger OppportunityProductInsert on Opportunity (after Insert, after Update )
{
    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 Pricebook2Id=:PB.Id limit 1 ];
    List<Opportunity> lstOpportunity = new List<Opportunity>();
    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 && a.CurrencyIsoCode == P.CurrencyIsoCode)
            {
                OpportunityLineItem OppLI = new OpportunityLineItem (OpportunityId=a.id, UnitPrice=0, Quantity =1, PricebookEntryId=P.Id);
                lstOpportunity.add(OppLI);
            }
        }
    }
    if(!lstOpportunityisEmpty())
        update lstOpportunity;
}

----------------------------------------------------------------------------------------------------------------------------------