+ Start a Discussion
Redmanx03Redmanx03 

System.LimitException: Too many SOQL queries: 101

Hello All recently this error came up, i have not done anything but create a new trigger on opportunity. Any Idea Why this would fire off if my trigger is after update

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportunityLineItemUniqueID: execution of BeforeUpdate

caused by: System.LimitException: Too many SOQL queries: 101

Trigger.OpportunityLineItemUniqueID: line 7, column 1: []

 

Trigger:

trigger OpportunityLineItemUniqueID on OpportunityLineItem (before insert, before update) {
    Set<Id> OpportunityIDs = new Set<Id>();
    for (OpportunityLineItem oli : Trigger.New) {
        OpportunityIDs.add(oli.OpportunityId);
    }
    
    Map<Id, Opportunity> Opportunities = new Map<Id, Opportunity>([SELECT Id, OrderId__c FROM Opportunity WHERE Id in :OpportunityIDs]);
    
    for (OpportunityLineItem oli : Trigger.New) {
        Opportunity opp = Opportunities.get(oli.OpportunityId);
        if (opp == null) continue;
        if (opp.OrderId__c == null || opp.OrderId__c == '') continue;
        if (oli.Sequence__c == null || oli.Sequence__c == '') continue;
        
        oli.ScheduleId__c = opp.OrderId__c + '.' + oli.Sequence__c;
    }
}

 

crop1645crop1645

Redmanx03

 

Does your 'new' trigger on Opportunity do inserts of OpportunityLineItems in a for loop rather than as a bulk insert of a list of OpportunityLineItems?  This would cause the OpportunityLineItem trigger to get invoked many times and eventually hit the too many SOQL calls governor limit

 

 

Niket SFNiket SF
Is there any other trigger on OpportunityLineItem ?

Here may be possible that trigger is going in recursion.
Redmanx03Redmanx03

That is only trigger which creates Opportunity Line Items.

Redmanx03Redmanx03

My new trigger does not create Line items. It does however create a new task for any opportunity that is closed-won. When created as closed-won or updated to closed-won

crop1645crop1645

Redmanx03

 

Go to Setup | Monitoring | Debug Logs and turn on the debug log for the running user. Switch off unnecessary noise in the log (like set System to None). Repeat your test. Look at the log. Every SOQL call will be logged and you'll be able to see why 101 SOQL queries are done in this test.

RockzzRockzz

Hi ...

 

Try below Code ....

 

trigger OpportunityLineItemUniqueID on OpportunityLineItem (before insert, before update) {
    Set<Id> OpportunityIDs = new Set<Id>();
    for (OpportunityLineItem oli : Trigger.New) {
        OpportunityIDs.add(oli.OpportunityId);
    }
    
    Map<Id, Opportunity> Opportunities = new Map<Id, Opportunity>([SELECT Id, OrderId__c FROM Opportunity WHERE Id in :OpportunityIDs limit 1]);
    
    for (OpportunityLineItem oli : Trigger.New) {
        Opportunity opp = Opportunities.get(oli.OpportunityId);
        if (opp == null) continue;
        if (opp.OrderId__c == null || opp.OrderId__c == '') continue;
        if (oli.Sequence__c == null || oli.Sequence__c == '') continue;
        
        oli.ScheduleId__c = opp.OrderId__c + '.' + oli.Sequence__c;
    }
}

Thanks,
Cool Sfdc