I have created a trigger that acts both before update and before insert but the insert side of my trigger works fine but when i try to update i get this error:
Apex trigger LineItemCreation caused an unexpected exception, contact your administrator: LineItemCreation: execution of BeforeUpdate caused by: System.DmlException: Delete failed. First exception on row 0 with id 00o9E000000KhACQA0; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 00k9E000002xUyx) is currently in trigger LineItemCreation, therefore it cannot recursively update itself: []: Trigger.LineItemCreation: line 90, column 1
The after update part of my trigger it referencing too is like so:
if(trigger.isUpdate && trigger.isBefore){
List<OpportunityLineItemSchedule> getSchedules = new List<OpportunityLineItemSchedule>();
getSchedules = [SELECT Id, Revenue FROM OpportunityLineItemSchedule
WHERE OpportunityLineItemId =:OLI.Id];
System.debug('Update actioned' + getSchedules);
if(!getSchedules.isEmpty()){
System.debug('Items Delete' + GetSchedules);
delete getSchedules;
System.debug('Gone' + GetSchedules);
}
}
My whole code is here, also any improvements you can offer me in general or best practice i would really appreciate as ive only just started learning apex.
trigger LineItemCreation on OpportunityLineItem (after insert,before update, after update) {
for(OpportunityLineItem OLI :Trigger.new){
if(trigger.isInsert && trigger.isAfter){
Product2 productsRef = new Product2();
Opportunity oppRef = new Opportunity();
System.Debug('Opportunity Line Quantity = ' + OLI.Quantity);
try{
productsRef = [SELECT Id, Basis__c, Family FROM Product2 WHERE
Id =:OLI.Product2Id];
System.Debug('product List created = ' + productsRef);
}
catch(Exception e){
System.debug('The following exception has occurred: ' + e.getMessage());
}
oppRef = [SELECT Id, Contract_Start_Date__c,
Contract_End_Date__c FROM Opportunity
WHERE Id=:OLI.OpportunityId];
System.Debug('Opportunity List created = ' + oppRef);
System.Debug('Opportunity List created = ' + OLI.TotalPrice);
System.Debug('Opportunity Line Quantity = ' + OLI.Quantity);
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);
List<OpportunityLineItemSchedule> OLISlist = new List<OpportunityLineItemSchedule>();
Decimal totalPaid = 0;
for(Integer i=0; i < daysDiff; i++){
System.Debug('for loop');
if(productsRef.Basis__c =='Monthly' && productsRef.Family=='Licensing'){
OpportunityLineItemSchedule OLISl = new OpportunityLineItemSchedule(
OpportunityLineItemId=OLI.Id,
Revenue=OliDayPrice,
ScheduleDate=(oppRef.Contract_Start_Date__c + i), //issue with it adding on revenue to saleprice
Type='Revenue');
OLISlist.add(OLISl);
System.Debug(OLISlist);
System.Debug('Inc' + totalPaid);
}
}
System.debug('Total : ' + totalPaid);
if(!OLISlist.isEmpty()){
insert OLISlist;
System.debug('List Inserted:' + OLISlist);
}
}
if(trigger.isUpdate && trigger.isBefore){
List<OpportunityLineItemSchedule> getSchedules = new List<OpportunityLineItemSchedule>();
getSchedules = [SELECT Id, Revenue FROM OpportunityLineItemSchedule
WHERE OpportunityLineItemId =:OLI.Id];
System.debug('Update actioned' + getSchedules);
if(!getSchedules.isEmpty()){
System.debug('Items Delete' + GetSchedules);
delete getSchedules;
System.debug('Gone' + GetSchedules);
}
}
}
}