I have written a trigger for before update and after insert function
trigger EstimateBudgetPerMonth on Opportunity__c(after insert, before update) {
Date startDate = Date.newInstance(2008, 1, 1);
Date endDate = Date.newInstance(2008, 1, 30);
Set < Id > oppId = new Set < Id > ();
Integer totalBudget;
for (Opportunity__c opp: Trigger.new) {
startDate = opp.Campaign_Start_Date1__c;
endDate = opp.Campaign_End_Date1__c;
totalBudget = Integer.valueOf(opp.Budget1__c);
oppId.add(opp.Id);
}
InsertBudgetOpp insertRec = new InsertBudgetOpp();
InsertBudgetOpp updateRec = new InsertBudgetOpp();
if (Trigger.isInsert) {
System.debug('--===Insert==--#--===Insert==--#--===Insert==--');
List < Budget_Allocation__c > baList = insertRec.insertChild(oppId, startDate, endDate, totalBudget);
insert baList;
} else if (Trigger.isUpdate) {
System.debug('--===Update==--#--===Update==--#--===Update==--');
Map < String, Budget_Allocation__c > baNewMap = updateRec.updateChild(oppId, startDate, endDate, totalBudget);
upsert baNewMap.values();
}
}
Its throwing the below error.
Error: Invalid Data. Review all error messages below to correct your data. Apex trigger EstimateBudgetPerMonth caused an unexpected exception, contact your administrator: EstimateBudgetPerMonth: execution of BeforeUpdate caused by: System.DmlException: Upsert failed. First exception on row 0 with id a0Up00000000FHJEA2; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = a06p0000000N3WU) is currently in trigger EstimateBudgetPerMonth, therefore it cannot recursively update itself: []: Trigger.EstimateBudgetPerMonth: line 38, column 1