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
S Balaji 8S Balaji 8 

Can someone help me how to actually update parent field MOD_Week__c on order detail object.

I am getting error saying ord2.MOD_Email_Sent_Date_DR__c this is not valid variable.


================================================
public static void update2WeekPickList(List<MOD_Order_Detail__c> ordetaillist)
   {
       Set<Id>OrderDetailIds=new Set<Id>();
       for(MOD_Order_Detail__c ord:ordetaillist)
       {
           OrderDetailIds.add(ord.Id);
       }
       List<MOD_Order_Detail__c> ordetaillist2=[Select id,MOD_Email_Sent_Date_DR__c,MOD_Week__c,(SELECT Id,EffectiveDate FROM Orders1__r)
                                                                                                 from MOD_Order_Detail__c where Id=:OrderDetailIds];
       List<MOD_Order_Detail__c> UpdatedOrdDetailList=new List<MOD_Order_Detail__c>();
       Map<Id,List<Order>> OrderMap=new Map<Id,List<Order>>();
       for(MOD_Order_Detail__c ord2 :ordetaillist2)
       {
        OrderMap.put(ord2.Id, new List<Order>{ord2.Orders1__r});
       }      
         for(Order orr:OrderMap.values())
         {
            //dateCalculation2w(orr.EffectiveDate);
            Date ThirdMondayafterStartDate =orr.EffectiveDate.toStartofWeek() + 22 ;
            Date FifthMondayafterStartDate= orr.EffectiveDate.toStartofWeek() + 36 ;
            Date SeventhMondayafterStartDate=orr.EffectiveDate.toStartofWeek() + 50 ;
            Date NinthMondayafterStartDate= orr.EffectiveDate.toStartofWeek() + 64 ;
            Date ThirtneenthMondayafterStartDate= orr.EffectiveDate.toStartofWeek() + 92 ;
            Date SeventeenthMondayafterStartDate= orr.EffectiveDate.toStartofWeek() + 120 ;
            Date TwentyOneMondayafterStartDate= orr.EffectiveDate.toStartofWeek() + 148 ;
            Date TwentyFiveMondayafterStartDate= orr.EffectiveDate.toStartofWeek() + 176 ;
           
            If( orr.EffectiveDate < ord2.MOD_Email_Sent_Date_DR__c && ord2.MOD_Email_Sent_Date_DR__c <= ThirdMondayafterStartDate)
            {
                for (MOD_Order_Detail__c OrdOriginal:ordetaillist)
                {
                    MOD_Order_Detail__c ModOrdDetail=new MOD_Order_Detail__c();
                    ModOrdDetail.MOD_Week__c='2W';
                    ModOrdDetail.ID=OrdOriginal.ID;
                    UpdatedOrdDetailList.add(ModOrdDetail);
                }    
            }
         }
           if(!UpdatedOrdDetailList.isEmpty())
           {
               Update UpdatedOrdDetailList;
           }
      }
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Balaji,

The for loop variable is not accessible  after the for loop ends. In your code ord2 is for loop iteration variable 

  for(MOD_Order_Detail__c ord2 :ordetaillist2)
       {
        OrderMap.put(ord2.Id, new List<Order>{ord2.Orders1__r});
       }      

You are trying to access this out of for loop which is causing the issue.

If this solution helps, Please mark it as best answer.

Thanks,