function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ramesh VaratharajRamesh Varatharaj 

oppotunitylineitemschedule resetting scheduledate based on servicedate

Hi Team, I have written a code to reset the schedule date whenever the opportunityproductlineitem's service date gets updated. I am coming across an issues - assume opportuntiy has 5 productlineitem and when i edit the servicedate from "editall" page, the system triggers the scheduledate updates only for the last productlineitem's realted schedules - ie the last item in the product. But the updated servicedate gets saved for all the product line item, but the trigger to update the lineitem's schedule is getting triggered only for the last product. But when i go and just click "Edit" and "Save" for other products individually, the schedule gets updated. 

Any idea why this is happening? Thanks 
Ramesh VaratharajRamesh Varatharaj
Team, Here is the exact code i am talking about. I am expecting this to update the schedule date when service date is udpated at the opportunityproductitem. This works find when we update individual productitem, but if we use "Edit All" function part of opportunity product, this is not working as expected. Say if i have 5 opportunity product line items mapped to an opportunity. when i edit the service dates with "Edit All", only the scheduledates associated with the last productlineitem appearing on the edit all list, is getting udpated. please help. 


trigger lineitemschedule1 on OpportunityLineItem (after Insert ,after Update) {
    List<OpportunityLineItemSchedule> itemscheduleA = new List<OpportunityLineItemSchedule>();
       
     if(Trigger.isUpdate){
        
        for(OpportunityLineItem  p: trigger.New) {
            OpportunityLineItem  pOld=  Trigger.oldMap.get(p.Id);                
                                    
                Date dt= p.ServiceDate;
                Integer mon=dt.month();
                Integer d=dt.day();
                Integer y=dt.year();
                Integer DaysInMonth=date.daysInMonth(y,mon);        
                boolean flag=false;

                Date dt1 = date.newInstance(y,mon,d);
                Date dt2 = date.newInstance(y,mon+1,1);

                Integer i =1 ; 
                itemscheduleA.clear();
                itemscheduleA=[Select id, ScheduleDate from OpportunityLineItemSchedule where OpportunityLineItemId =: p.Id ORDER BY ScheduleDate ASC];
                                 
                for(OpportunityLineItemSchedule rs : itemscheduleA) {
                    if(flag==false) {
                        rs.ScheduleDate =dt1;
                        flag = true;
                    }
                    
                    else {
                        rs.ScheduleDate =dt2;                        
                        dt2 = dt2.addMonths(i);

                    }
                }
                
            } 
        
     
             
        }
         
         update itemscheduleA;
         
     }
Ramesh VaratharajRamesh Varatharaj
i got the issue and fixed it. the update comment was outside the loop. so created another list to store the records for udpate and performed DML with that list.