2

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();
if(trigger.isInsert){
    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(
                                              OpportunityLineItemId=OLI.Id,
                                              Revenue=OliDayPrice,
                                              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');
        }

    }
5
  • 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
    Commented Nov 24, 2016 at 19:02
  • Are you meant to be using the getSchedules list at some stage?
    – TSmith
    Commented Nov 24, 2016 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
    Commented Nov 24, 2016 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
    Commented Nov 25, 2016 at 5:40
  • No errors all works perfectly, I'll do what you all suggested she I get to work
    – Nebbyyy
    Commented Nov 25, 2016 at 8:24

1 Answer 1

0

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.

2
  • So what are my options to get around it and incrementally create records?
    – Nebbyyy
    Commented Nov 24, 2016 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
    Commented Nov 25, 2016 at 5:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .