+ Start a Discussion
Jennifer LaingJennifer Laing 

Help with SOQL outside of FOR loop

Hello

I'm hoping someone can help me as I have very little developer experience!

I'm receiving the Too many SOQL queries: 101 error.  The code is below.  Can someone please advise how I can fix this?  Any help would be greatly appreciated!  I've removed most of the commented out statements.

trigger OpportunityLineItemTrigger on OpportunityLineItem (after delete, before insert, after insert, before update,after update) {
    
    if(Trigger.isAfter){
        if(Trigger.isDelete){
            // Flag opps having OLIs with different end dates
            OpportunityLineItemTriggerMethods.checkLineItemEndDates(trigger.old);

            //delete related RevenueScheduleItems
            OpportunityLineItemTriggerMethods.removeRevenueScheduleItems(trigger.old);
            
            // Create Deleted_OpportunityLineItem objects, for the Cast Iron integration.
            OpportunityLineItemTriggerMethods.createDeletedLineItemObjects(trigger.old);
            

        }else if(Trigger.isUpdate){
            //copy the modified fields from OLI to related RSI records
            OpportunityLineItemTriggerMethods.updateRevenueScheduleItems(trigger.newMap,trigger.oldMap);
            
            // Add any new revenue schedule items, if the term is increased and it's a closed consulting opportunity line item.
            OpportunityLineItemTriggerMethods.createAdditionalRevenueScheduleItems(trigger.new, trigger.oldMap);
        }
    }   
    
    if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)){
        
        
        OpportunityLineItemTriggerMethods.opportunityLineItemExchangeRateStamping(Trigger.new, Trigger.oldMap);
          
        OpportunityLineItemTriggerMethods.calculatePPLTotalPrice(Trigger.isInsert, Trigger.new, Trigger.oldMap);
        for(OpportunityLineItem oli:Trigger.New)
        {
          
            Double upLift = oli.Uplift_Percentage__c == null ? 0.0 : oli.Uplift_Percentage__c;
            Double term = oli.term__c == null ? 0.0 : oli.term__c;
            
            Double multiplier = (1.0 + (upLift/100)) * term * 
                    (oli.Company_type_factor__c == null || oli.Company_type_factor__c == 0.0 ? 1.0 : oli.Company_type_factor__c) *
                    (oli.Company_size_factor__c == null || oli.Company_size_factor__c == 0.0 ? 1.0 : oli.Company_size_factor__c);
            Double ppm = oli.Previous_Price_Per_Month__c == null ? 0.0 : oli.Previous_Price_Per_Month__c;
            Double listPrice = oli.ListPrice == null ? 0.0 : oli.ListPrice;
            Double researchAmount = oli.Research_Amount_Price__c == null ? 0.0 : oli.Research_Amount_Price__c;
            Double scheduledDiscount = oli.Scheduled_Discount__c == null ? 0.0 : oli.Scheduled_Discount__c;
            Double unitPrice = oli.UnitPrice == null ? 0.0 : oli.UnitPrice;
            
            oli.Research_Amount_Price__c = (ppm == 0.0) ? (listPrice / 12) * multiplier :ppm * multiplier;
            Double researchAmount1 = oli.Research_Amount_Price__c;
            oli.Discount_Gap__c = (listPrice == 0) ? 0 : researchAmount1 * (1-(scheduledDiscount/100)) - unitPrice; 
        }
    }
    
      if(Trigger.isBefore && Trigger.isInsert){ 
        OpportunityLineItemTriggerMethods.defaultStartDate(Trigger.new);
    }
    
    // 15/06/2012 DP Update
    if (Trigger.isAfter && Trigger.isInsert) {
        OpportunityLineItemTriggerMethods.createRevenueScheduleItems(Trigger.new);
    }
    
    if(Trigger.isAfter ){
        // automatically apply scheduled discounts 
        // future methods can't take sObject arguments .
        OpportunityLineItemTriggerMethods.checkScheduledDiscounts(trigger.new,trigger.oldMap);
        
        // Flag opps having OLIs with different end dates
        if (!Trigger.isDelete){
            OpportunityLineItemTriggerMethods.checkLineItemEndDates(trigger.new);       
        }
    }
    
}
Deepak GulianDeepak Gulian
Please paste the code of this Apex Class "OpportunityLineItemTriggerMethods"
Jennifer LaingJennifer Laing
Thank you for your reply Deepak.  

There are too many characters to post!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Deepak GulianDeepak Gulian
I reviewed your trigger code, but it seems there is no SOQL in trigger so possibly the apex class is having that error code. So if its possible, share code on this email deepak.gulian007@gmail.com
Jennifer LaingJennifer Laing
I've emailed it to you now, thank you.