+ Start a Discussion
Nagaraju Mogili 22Nagaraju Mogili 22 

When I am updating a child record in child object, some fields like start city and start date should not change for same record in the parent object, how can acheive this, anyone help me on this ?

public class UpdateParentFields {
    public static void AfterInsert(List<Object_B__c> blist){
      List<Id> bid = new List<Id>();        
        for(Object_B__c b :blist){
             bid.add(b.Object_A__c); 
        }
    Map<Id,Object_A__c> Parentmap = new Map<Id,Object_A__c>([Select Id,End_City__c,End_Date__c,Start_City__c,Start_Date__c,(select Id,Arrive_City__c,Departure_City__c,End_Date__c,Start_Date__c from Obj_B__r) from Object_A__c where Id IN : bid]);   
               for(Object_B__c c : blist){
                Object_A__c objA = Parentmap.get(c.Object_A__c);
                      objA.Start_City__c = c.Arrive_City__c;
                      objA.End_City__c = c.Departure_City__c;
                      objA.Start_Date__c = c.Start_Date__c;
                      objA.End_Date__c = c.End_Date__c;
                   if(c.Arrive_City__c == c.Departure_City__c){
                       c.Departure_City__c.adderror('You should not select the same city for Start city and End City');
                   }
                    }
                    update parentmap.values();        
            }
    public static void AfterUpdate(List<Object_B__c> oblist){
                         List<Id> bid = new List<Id>();        
                         for(Object_B__c b : oblist){
                         bid.add(b.Object_A__c); 
             }
    Map<Id,Object_A__c> Parentmap = new Map<Id,Object_A__c>([Select Id,End_City__c,End_Date__c,Start_City__c,Start_Date__c,(select Id,Arrive_City__c,Departure_City__c,End_Date__c,Start_Date__c from Obj_B__r) from Object_A__c where Id IN : bid]);   
              for(Object_B__c objb : oblist){
                               Object_A__c objA = parentmap.get(objb.Object_A__c);
                               if(objA.End_City__c != objb.Departure_City__c){
                                   objA.End_City__c = objb.Departure_City__c;
                               }
                               if(objA.End_Date__c != objb.End_Date__c){
                                   objA.End_Date__c = objb.End_Date__c;
                       }     
                  if(objb.Arrive_City__c == objb.Departure_City__c){
                       objb.Departure_City__c.adderror('You should not select the same city for Start city and End City');
                   }
               }
            Update Parentmap.values();
         }
}