+ Start a Discussion
Matt FieldMatt Field 

Fields not updating on refresh

I have a trigger that we use to calculate annual revenue out to 4 years on an opportunity.  It works fine except when the opportunity revenue is changed.  When the amount is changed, I have found that the 3rd and 4th year revenue does not update.  Can someone look at this and tell me what the problem is?

 

trigger reestablishServiceSchedule on Opportunity (after update) {

    Set<Id> oppIds = new Set<Id>();
    List<OpportunityLineItemSchedule> schedules = new List<OpportunityLineItemSchedule>();
    List<OpportunityLineItem> olis = new List<OpportunityLineItem>();

    if(!ProcessorControl.isTriggered){
        
        for(Integer i = 0; i < Trigger.new.size(); i++){
             if(Trigger.new[i].CloseDate!=Trigger.old[i].CloseDate || (Trigger.new[i].Amount!=Trigger.old[i].Amount)){
                oppIds.add(Trigger.new[i].Id);
            }
        }
        
        
        for (OpportunityLineItem oli: [Select Id, Opportunity.CloseDate, ServiceDate, First_Year_Revenue__c, (Select Id, ScheduleDate, Revenue From OpportunityLineItemSchedules Order By ScheduleDate Asc) From OpportunityLineItem Where HasRevenueSchedule = true AND Opportunity.ID IN: oppIds]){
            Date closeDate = oli.Opportunity.CloseDate;
            for(Integer i = 0; i < oli.OpportunityLineItemSchedules.size(); i++){
                oli.OpportunityLineItemSchedules[i].ScheduleDate = closeDate.addMonths(i);
                schedules.add(oli.OpportunityLineItemSchedules[i]);
            }
            oli.ServiceDate = oli.Opportunity.CloseDate;
            olis.add(oli);
        }
        update olis;
        update schedules;
        
        olis = new List<OpportunityLineItem>();
        for (OpportunityLineItem oli: [Select Id, Opportunity.CloseDate, First_Year_Revenue__c, TotalPrice, HasRevenueSchedule, (Select Id, ScheduleDate, Revenue From OpportunityLineItemSchedules Order By ScheduleDate Asc) From OpportunityLineItem Where Opportunity.ID IN: oppIds]){
            double firstYearRevenue = 0;
            double secondYearRevenue = 0;
            double thirdYearRevenue = 0;
            double FourthYearRevenue = 0;
            if(oli.HasRevenueSchedule){
                for(OpportunityLineItemSchedule sched: oli.OpportunityLineItemSchedules){
                    if(oli.Opportunity.CloseDate.year() == sched.ScheduleDate.year()){
                            firstYearRevenue += sched.Revenue;
                    } else if(oli.Opportunity.CloseDate.year()+ 1 == sched.ScheduleDate.year()){
                            secondYearRevenue += sched.Revenue;
                    } else if(oli.Opportunity.CloseDate.year()+ 2 == sched.ScheduleDate.year()){
                            thirdYearRevenue += sched.Revenue;
                    } else if(oli.Opportunity.CloseDate.year() + 3 == sched.ScheduleDate.year()){
                            FourthYearRevenue += sched.Revenue;
                    }
                }
            } else {
                firstYearRevenue = oli.TotalPrice;
            }
            oli.First_Year_Revenue__c = firstYearRevenue;
            oli.Second_Year_Revenue__c = secondYearRevenue;
            oli.Third_Year_Revenue__c = thirdYearRevenue;
            oli.Fourth_Year_Revenue__c = FourthYearRevenue;
            olis.add(oli);
        }
        update olis;
        
            }

}

 I appreciate any help that you can give me.

 

Thanks,

 

Matt

Best Answer chosen by Admin (Salesforce Developers) 
Marko LamotMarko Lamot

I would try to put 

Second_Year_Revenue__c,Third_Year_Revenue__c, Fourth_Year_Revenue__c in your second for-select statement

All Answers

Marko LamotMarko Lamot

I would try to put 

Second_Year_Revenue__c,Third_Year_Revenue__c, Fourth_Year_Revenue__c in your second for-select statement
This was selected as the best answer
Matt FieldMatt Field

Thanks Marko. That fixed it.