+ Start a Discussion
Ramu kolliRamu kolli 

Update parent object's, based on date field in child object

I was trying to pick up date field in child object(start date and end date). if the period falls in between October month then, i need to set parent object's checkbox to true. i wanted to perform this operation while inserting/updating records in child object. shall i use a trigger to perform this operation or simple class to be called from trigger. please help me
Andy BoettcherAndy Boettcher
You should be able to do this with a simple Process Builder or Workflow Field Update - just use the "MONTH()" function to update the parent record's checkbox.
karunakarreddy bade 8karunakarreddy bade 8
Hi Ramu kolli,

Update parent object's, based on date field in child object intwo ways

1.Using Trigger to update parent object value with child value 

example of trigger:

public class UpdateParent
{
 public static void updateChild(List<Case> newTrigger) {
 Set<Case> caseUpdate = new Set<Case>();
 for (Case c : newTrigger) {
 if(Trigger.oldMap.get(c.Id).get('Datetime_Required__c') != c.Datetime_Required__c && c.ParentId != NULL) {
 Case newCase = new Case( Datetime_Required__c = c.Datetime_Required__c,  Id = c.ParentId );
 caseUpdate.add(newCase);
 }
 }
List<Case> caseList = New List<Case>(caseUpdate);
 update caseList;
 }
}


2.using the Workflow Field Update parent object value with child value
Gyanender SinghGyanender Singh
Hi Ramu,

Use this code and this fill solve your isssue.
 
trigger CheckParentCheckbox on ChildObject__c (after insert, after update) { 
  Map<ID, ParentObject> parentOpps = new Map<ID, ParentObject>(); 
  List<Id> listIds = new List<Id>();

  for (ChildObject__c childObj : Trigger.new) {
    listIds.add(childObj.ParentObject__c);
  }

  parentOpps = new Map<Id, ParentObject>([SELECT id,Checkboxfield__c,  (SELECT ID,Start_Date__c,End_Date__c FROM ChildObjects__r where Start_Date__c<= this_month and End_Date__c >= this_month) FROM ParentObject WHERE ID IN :listIds]);

  for (ChildObject__c ChildObject: Trigger.new){
     ParentObject myParentOpp = parentOpps.get(ChildObject.ParentObject__c);
     myParentOpp.Checkboxfield__c = true ;
  }

  update parentOpps.values();
}

if this is the solution of your problem so please choose this as the Best Anser.

Thanks
Gyani.
Gyanender SinghGyanender Singh
Hi Ramu,

Slightly change in this trigger i miss one validation in the trigger so please modify the code with the following code.
trigger CheckParentCheckbox on ChildObject__c (after insert, after update) { 
  Map<ID, ParentObject> parentOpps = new Map<ID, ParentObject>(); 
  List<Id> listIds = new List<Id>();

  integer dt = Date.Today().Month();

  for (ChildObject__c childObj : Trigger.new) {
    listIds.add(childObj.ParentObject__c);
  }

  parentOpps = new Map<Id, ParentObject>([SELECT id,Checkboxfield__c,  (SELECT ID,Start_Date__c,End_Date__c FROM ChildObjects__r where Start_Date__c<= this_month and End_Date__c >= this_month) FROM ParentObject WHERE ID IN :listIds]);

  for (ChildObject__c ChildObject: Trigger.new){
    if(ChildObject.Start_Date__c.month() == dt && quote.End_Date__c.month() == dt){
     ParentObject myParentOpp = parentOpps.get(ChildObject.ParentObject__c);
if(ChildObject.Start_Date__c.month() == dt && ChildObject.End_Date__c.month() == dt){
     myParentOpp.Checkboxfield__c = true ;
}
else{
         myParentOpp.Checkboxfield__c = false;
     }
  }

  update parentOpps.values();
}

Sorry for inconvience please you this code.

Thanks
Gyani.
Ramu kolliRamu kolli
Hello Gyan, Thanks for your input. let me try with your code. 

regarding Workflow field update, I think we can't update parent based on the child values. because while we are creating workflow, if we select child object, we can make changes in child object only.

please clarify if i am wrong.

Thanks,
Ramu.
Shalagha GumberShalagha Gumber
Hi Ramu,

Workflow field updates can update fields on the parent object also, provided the parent has a master-detail (and not lookup) relationship with the child object.
Gyanender SinghGyanender Singh
Hi Ramu,

In workflow we can update parent field from the child object but we can not make any changes in child from parent object. When you create workflow on the child object the you have the lookup object and you can select any field from the parent object.

Thanks 
Gyani.
Ramu kolliRamu kolli
Hi Gyani,

I was trying to execute the below code, but getting below mentioned error. 

trigger UpdateCheckbox on Account_Child__c(after insert, after update) {
  Map<ID, Account> parentOpps = new Map<ID, Account>();
  List<Id> listIds = new List<Id>();
  for (Account_Child__c childObj : Trigger.new) {
    listIds.add(childObj.Account__c);
  }

  parentOpps = new Map<Id, Account>([SELECT id, Active__c,  (SELECT ID,Start_date__c,End_date__c FROM Account_Child__r where Start_date__c<= this_month and End_date__c >= this_month) FROM Account WHERE ID IN :listIds]);

  for (Account_Child__c ChildObject: Trigger.new){
        Account myParentOpp = parentOpps.get(ChildObject.Account__c);
         myParentOpp.Active__c = true ;
  }

  update parentOpps.values();

}


Error is :
 Compile Error: Didn't understand relationship 'Account_Child__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 17 column 37
Gyanender SinghGyanender Singh
Hi Ramu,

User Child relationship name here of your object and if you have any other doubt then show me your code.

Thanks
Gyani.