I have created an apex trigger so that it will create a record for everyday of an opportunities contract, I have run the code without the insert statement and my debug shows it works fine. What am I missing and why wont my record insert when i add the statement into my for loop. Thank you.

trigger LineItemCreation on OpportunityLineItem (after insert) {
List<OpportunityLineItemSchedule> getSchedules = new List<OpportunityLineItemSchedule>();
Product2 productsRef = new Product2();
Opportunity oppRef = new Opportunity();
    for(OpportunityLineItem OLI :Trigger.new){

        try {
        productsRef = [SELECT Id, Basis__c, Family FROM Product2 WHERE
                                   Basis__c ='Monthly' and Family='Licensing' 
                                   and Id =:OLI.Product2Id]; 
        System.Debug('product List created = ' + productsRef);

        oppRef = [SELECT Id, Contract_Start_Date__c, 
                              Contract_End_Date__c FROM Opportunity
                              WHERE Id=:OLI.OpportunityId];
        System.Debug('Opportunity List created = ' + oppRef);

        Integer daysDiff = oppRef.Contract_Start_Date__c.daysBetween(oppRef.Contract_End_Date__c);
        System.Debug('DaysDifference = ' + daysDiff);

        Decimal OliDayPrice = (OLI.TotalPrice / daysDiff);
        System.Debug('Dailys Schedule Record Price = ' + oliDayPrice);

        for(Integer i=0; i <= daysDiff; i++){
        if(productsRef.Basis__c =='Monthly' && productsRef.Family=='Licensing'){
            OpportunityLineItemSchedule OLIS = new OpportunityLineItemSchedule(
                                              ScheduleDate=oppRef.Contract_Start_Date__c + i);
            insert OLIS; //(ISSUE IS HERE)
            System.debug('Revenue Schedule Created =' + OLIS + 'increment = ' + i);
        else return;
        catch (Exception e) {
            System.debug('No revenue record created');

  • There is a great module in Trailhead that goes over triggers here - trailhead.salesforce.com/en/apex_triggers/apex_triggers_intro. The 2nd unit talks about bulkification of Triggers. This blog is awesome for learning about collections - blog.jeffdouglas.com/2011/01/06/fun-with-salesforce-collections
    – Jenny B
    Nov 24 '16 at 19:02
  • Are you meant to be using the getSchedules list at some stage?
    – TSmith
    Nov 24 '16 at 22:48
  • That's for me deleting all the data I insert at a later stage when I add a before update element to it
    – Nebbyyy
    Nov 24 '16 at 22:50
  • The code is not bulkified.Querying inside a for loop and using a DML statement inside the for loop is not recommended.Please bulkify your code.Are you getting any error in the above code?
    – Preya
    Nov 25 '16 at 5:40
  • No errors all works perfectly, I'll do what you all suggested she I get to work
    – Nebbyyy
    Nov 25 '16 at 8:24

It is strictly recommend that queries/DML operations should not be within a loop. You are getting error as it is crossing it's governor limits.

If you are new to salesforce, please read about governor limits and best practices.

  • So what are my options to get around it and incrementally create records?
    – Nebbyyy
    Nov 24 '16 at 18:41
  • Instead of making a dml stmt in for loop create a list and add the object in the list and once the for loop iteration is over insert that particular list outside the for loop.
    – RCS
    Nov 25 '16 at 5:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.