You need to sign in to do that
Don't have an account?

Trigger Recursion Solution - Flipping a Field Flag Back
I have a trigger that works when creating and Opportunity that creates a record for each quarter and divides up the revenue. What I would like to do is to flip the flag that indicates to run auto calculate / generate trigger back to no after the records are updated in a new Custom Object called Opportunity_Revenue__c, but I get a recursion error.
How can I get the field named: auto_generate_quarterly_forecast__c back to a no value after I run the insert trigger?
Trigger Code:
Thank you in Advance
Robert
How can I get the field named: auto_generate_quarterly_forecast__c back to a no value after I run the insert trigger?
Trigger Code:
trigger OpportunityRevenueTrigger on Opportunity (after insert, after update) { if(trigger.isInsert && trigger.isAfter){ OpportunityRevenueTriggerInsertHandler.OppQuarterlyRevInsert(trigger.new); }//END is.Insert if(trigger.isUpdate && trigger.isAfter){ OpportunityRevenueTriggerInsertHandler.OppQuarterlyRevDelete(trigger.new); OpportunityRevenueTriggerInsertHandler.OppQuarterlyRevInsert(trigger.new); }//END isUpdate }Trigger Handler Class :
public class OpportunityRevenueTriggerInsertHandler { public static void OppQuarterlyRevInsert(List<Opportunity> trig){ Date eDate; //this is the date that the projection should run through Date sDate; //this is the date that the projection should start Integer period; // Opp Contract_Length_Months__c is a decimal need to convert Decimal dRev; // Revenue per Day Decimal mRev; // Revenue per Month Decimal qRev; //Revenue per Quarter Integer dayDiff; Integer dayDiff2; List<Opportunity_Revenue__c> oppr = new List<Opportunity_Revenue__c>(); for(Opportunity opp : trig){ //Month Count Must be greater than 2 if(opp.amount > 0 && opp.contract_length_Months__c > 0 && opp.Auto_Generate_Quarterly_Forecast__c == 'Yes'){ sDate = opp.CloseDate; period = opp.contract_length_months__c.intValue(); eDate = opp.CloseDate.addMonths(period); dayDiff = sDate.daysBetween(eDate); System.debug('dayDiff '+dayDiff); dRev = opp.Amount /dayDiff; System.debug('dRev '+ dRev); mRev = opp.Amount / period; System.debug('mRev ' +mRev); qRev = mRev * 3; while (sDate < eDate){ Opportunity_Revenue__c oppr2 = new Opportunity_Revenue__c(); oppr2.Account__c = opp.AccountId; oppr2.Opportunity__c = opp.id; oppr2.Opportunity_Projected_Revenue__c = opp.Amount; oppr2.Month__c = sDate; if(opp.Contract_Length_Months__c > 2) { if(sDate.Month()== 1 || sDate.month()==2 || sDate.month() ==3) { dayDiff2 = sDate.daysBetween(eDate); System.debug('dayDiff2 '+ dayDiff2); if(sDate.daysBetween(eDate) < 90 ) { dayDiff2 = sDate.daysBetween(eDate); System.debug('dayDiff2 '+ dayDiff2); oppr2.Quarter__c = 'Quarter 1, '+string.valueOf(sDate.Year()); oppr2.ProjectedRevenue__c = dRev * sDate.daysBetween(eDate); } else { //END if(sDate.daysBetween(eDate) <90) dayDiff2 = sDate.daysBetween(eDate); System.debug('dayDiff2 '+ dayDiff2); oppr2.Quarter__c = 'Quarter 1, '+string.valueOf(sDate.Year()); oppr2.ProjectedRevenue__c = qRev;} }//END if (sDate.month()==Q1) if(sDate.Month()== 4 || sDate.month()==5 || sDate.month() ==6) { dayDiff2 = sDate.daysBetween(eDate); System.debug('dayDiff2 '+ dayDiff2); if(sDate.daysBetween(eDate) < 90 ) { dayDiff2 = sDate.daysBetween(eDate); System.debug('dayDiff2 '+ dayDiff2); oppr2.Quarter__c = 'Quarter 2, '+string.valueOf(sDate.Year()); oppr2.ProjectedRevenue__c = dRev * sDate.daysBetween(eDate); } else { //END if(sDate.daysBetween(eDate) <90) dayDiff2 = sDate.daysBetween(eDate); System.debug('dayDiff2 '+ dayDiff2); oppr2.Quarter__c = 'Quarter 2, '+string.valueOf(sDate.Year()); oppr2.ProjectedRevenue__c = qRev;} }//END if (sDate.month()==Q2) if(sDate.Month()== 7 || sDate.month()==8 || sDate.month() ==9) { dayDiff2 = sDate.daysBetween(eDate); System.debug('dayDiff2 '+ dayDiff2); if(sDate.daysBetween(eDate) < 90 ) { dayDiff2 = sDate.daysBetween(eDate); System.debug('dayDiff2 '+ dayDiff2); oppr2.Quarter__c = 'Quarter 3, '+string.valueOf(sDate.Year()); oppr2.ProjectedRevenue__c = dRev * sDate.daysBetween(eDate); } else { //END if(sDate.daysBetween(eDate) <90) dayDiff2 = sDate.daysBetween(eDate); System.debug('dayDiff2 '+ dayDiff2); oppr2.Quarter__c = 'Quarter 3, '+string.valueOf(sDate.Year()); oppr2.ProjectedRevenue__c = qRev;} }//END if (sDate.month()==Q3) if(sDate.Month()== 10 || sDate.month()==11 || sDate.month() ==12) { dayDiff2 = sDate.daysBetween(eDate); System.debug('dayDiff2 '+ dayDiff2); if(sDate.daysBetween(eDate) < 90 ) { dayDiff2 = sDate.daysBetween(eDate); System.debug('dayDiff2 '+ dayDiff2); oppr2.Quarter__c = 'Quarter 4, '+string.valueOf(sDate.Year()); oppr2.ProjectedRevenue__c = dRev * sDate.daysBetween(eDate); } else { //END if(sDate.daysBetween(eDate) <90) dayDiff2 = sDate.daysBetween(eDate); System.debug('dayDiff2 '+ dayDiff2); oppr2.Quarter__c = 'Quarter 4, '+string.valueOf(sDate.Year()); oppr2.ProjectedRevenue__c = qRev;} }//END if (sDate.month()==Q4) }//END if(opp.Contract_Length_Months__c > 2) //if the Contract Length is less than a quarter if (opp.contract_length_months__c < 3){ if(sDate.Month()== 1 || sDate.month()==2 || sDate.month() ==3) { oppr2.Quarter__c = 'Quarter 1, '+string.valueOf(sDate.Year()); oppr2.ProjectedRevenue__c = opp.Amount; }//END if (sDate.month()==Q1) if(sDate.Month()== 4 || sDate.month()==5 || sDate.month() ==6) { oppr2.Quarter__c = 'Quarter 2, '+string.valueOf(sDate.Year()); oppr2.ProjectedRevenue__c = opp.Amount; }//END if (sDate.month()==Q2) if(sDate.Month()== 7 || sDate.month()==8 || sDate.month() ==9) { oppr2.Quarter__c = 'Quarter 3, '+string.valueOf(sDate.Year()); oppr2.ProjectedRevenue__c = opp.Amount; }//END if (sDate.month()==Q3) if(sDate.Month()== 10 || sDate.month()==11 || sDate.month() ==12) { oppr2.Quarter__c = 'Quarter 4, '+string.valueOf(sDate.Year()); oppr2.ProjectedRevenue__c = opp.Amount; }//END if (sDate.month()==Q4) }//END if (opp.contract_length_months__c < 3) sDate = sDate.addMonths(3); //increment month / Quarter oppr.add(oppr2); }//END while (sDate < eDate) }//END if(opp.amount > 0 && opp.contract_length_Months__c > 0) }//END for(Opportunity opp : trig) insert oppr; }//END OppQuarterlyRevInsert public static void OppQuarterlyRevDelete(List<Opportunity> trig) { List<ID> opprtnyId = new List<ID>(); //Loop through and get all the ids for(Opportunity opp: trig){ //Only put them in the List if you want them to be deleted if(Opp.Auto_Generate_Quarterly_Forecast__c == 'Yes'){ opprtnyId.add(opp.id); }//END if }//END for(Opportunity opp: trig) //Get the Opportunity_Revenue__c records in the table to delete List<Opportunity_Revenue__c> deleteOR = [SELECT id FROM Opportunity_Revenue__c WHERE Opportunity__c in: opprtnyID]; //if there are any records then delete them if(deleteOR.size() != 0){ Delete deleteOR; }//END if }//END OppQuarterlyRevDelete(List<Opportunity> trig) public static void FlipFlag(List<Opportunity> trig){ List<Opportunity> opp4 = new List<Opportunity>(); for(Opportunity opp3 : trig){ opp3.Auto_Generate_Quarterly_Forecast__c ='No'; opp4.add(opp3); } update opp4; } }//END OpportunityRevenueTriggerInsertHandlerI have to believe it is something simple I am missing, but dang I can not figure it out.
Thank you in Advance
Robert