+ Start a Discussion
ShirinShirin 

Error on triggers

Hi,

 

Being new to SFDC development , I am trying to write a apex trigger. On the opportunity page we have a field called number of resources. When the user enters the number, those many opportunity product records need to be created. I have written the code but i have been successful only when the number of resources is upto 15, if the number is above 15 , i get the following error. I have also added the code. I hope somebody can help me.

 

Thanks.

 

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

 

 

The error:

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger OppportunityProductInsert caused an unexpected exception, contact your administrator: OppportunityProductInsert: execution of AfterInsert 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 AfterInsert for [006N0000001qzQO] Opportunity trigger event AfterUpdate for [006N0000001qzQO] Opportunity trigger event AfterUpdate for [006N0000001qzQO] Opportunity trigger event AfterUpdate for [006N0000001qzQO] Opportunity trigger event AfterUpdate for [006N0000001qzQO] Opportunity trigger event AfterUpdate for [006N0000001qzQO] Opportunity trigger event AfterUpdate for [006N0000001qzQO] Opportunity trigger event AfterUpdate for [006N0000001qzQO] Opportunity trigger event AfterUpdate for [006N0000001qzQO] Opportunity trigger event AfterUpdate for [006N0000001qzQO] Opportunity trigger event AfterUpdate for [006N0000001qzQO] Opportunity trigger event AfterUpdate for [006N0000001qzQO] Opportunity trigger event AfterUpdate for [006N0000001qzQO] Opportunity trigger event AfterUpdate for [006N0000001qzQO] Opportunity trigger event AfterUpdate for [006N0000001qzQO] Opportunity trigger event AfterUpdate for [006N0000001qzQO]: []: Trigger.OppportunityProductInsert: line 12, column 1

 

 


dipudipu

The error message is very clear. There is a limit on Trigger depth(calling a trigger from another or same trigger). There has to be always limit on depth :). Check your logic.

 

You are probably looking to create new OpportunityLineItem only after insert. So try to put check like if (Trigger.isInsert) or whaterver suites you to block the recursive call.

Ankit AroraAnkit Arora

This scenario is known as cascading in salesforce. Please check your logic, your trigger is calling itself.

 

 

Thanks
Ankit Arora