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