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
subodh chaturvedi 17subodh chaturvedi 17 

How to change the value of child Record field (checkbox) Based on the parent Record Text Field

I have a field on parent (Card Program__c) object i.e  card_processor_status__c  & have a Child (Solution_participation__c) Object which has Active__c checkbox Field I want to Update the Checkbox Field  Whenever the card_processor_status__c   changes to  either Active or Inactive .
I tried to update this with process builder But it is No Success .How we can Update this through Code? 
Best Answer chosen by subodh chaturvedi 17
v varaprasadv varaprasad
Hi Subodh,

Please check once below sample code ,In the place of ParentFieldApiName add parent record API name.:
 
Trigger updateChildStatus on Card_Program__c(After update){
    Map<id,Card_Program__c> programIds = new Map<id,Card_Program__c>();
	for(Card_Program__c cp : trigger.new){
	if(cp.card_processor_status__c != null){
	    programIds.put(cp.id,cp);
	  }
	}
    
	List<Solution_participation__c> updChildrecs = new List<Solution_participation__c>();
	List<Solution_participation__c> childrecs = [select id,Active__c,ParentFieldApiName from Solution_participation__c where ParentFieldApiName in : programIds.keyset()];
    
	if(childrecs.size()>0){
	  for(Solution_participation__c sp : childrecs){
	    if(programIds.get(sp.ParentFieldApiName).card_processor_status__c == 'Active'){
		   sp.Active__c = True;
		}else {
           sp.Active__c = False;
         }		   
		   updChildrecs.add(sp);
	  }
	  if(updChildrecs.size()>0){
	      update updChildrecs;
	  }
	 
	
	}



}

Hope it helps you.


Thanks
Varaprasad

All Answers

v varaprasadv varaprasad
Hi Subodh,

Please check once below sample code ,In the place of ParentFieldApiName add parent record API name.:
 
Trigger updateChildStatus on Card_Program__c(After update){
    Map<id,Card_Program__c> programIds = new Map<id,Card_Program__c>();
	for(Card_Program__c cp : trigger.new){
	if(cp.card_processor_status__c != null){
	    programIds.put(cp.id,cp);
	  }
	}
    
	List<Solution_participation__c> updChildrecs = new List<Solution_participation__c>();
	List<Solution_participation__c> childrecs = [select id,Active__c,ParentFieldApiName from Solution_participation__c where ParentFieldApiName in : programIds.keyset()];
    
	if(childrecs.size()>0){
	  for(Solution_participation__c sp : childrecs){
	    if(programIds.get(sp.ParentFieldApiName).card_processor_status__c == 'Active'){
		   sp.Active__c = True;
		}else {
           sp.Active__c = False;
         }		   
		   updChildrecs.add(sp);
	  }
	  if(updChildrecs.size()>0){
	      update updChildrecs;
	  }
	 
	
	}



}

Hope it helps you.


Thanks
Varaprasad
This was selected as the best answer
subodh chaturvedi 17subodh chaturvedi 17
HI v varaprasad,

I tried Your Code But the Child Record field Active__c ( i.e Checkbox ) is not changed to uncheck when i changed my  card_processor_status__c  from Active to Inactive .
my requirement is when i changed my card_processor_status__c from Inactive  to Active then all  child records   Active__C (Checkbox) field should get check automatically & if My card_processor_status__c changed from Active to Inactive then Active__c should get Uncheck.
there are already a records present in object which i need to  change the Statuses & update the child record Accordingly. 

Modified code 
Public class CardProgramsolparticipationcheck{

 Public Static Void CheckFieldstatus(List<Card_Program__c> card){
    Map<id,Card_Program__c> programIds = new Map<id,Card_Program__c>();
    for(Card_Program__c cp : card){
    if(cp.card_processor_status__c != null){
        programIds.put(cp.id,cp);
      }
    }
    
    List<Solution_participation__c> updChildrecs = new List<Solution_participation__c>();
    List<Solution_participation__c> childrecs = [select id,Active__c,Card_Program__c from Solution_participation__c where Card_Program__c IN : programIds.keyset()];
    
    if(childrecs.size()>0){
      for(Solution_participation__c sp : childrecs){
        if(programIds.get(sp.Card_Program__c).card_processor_status__c == 'ACTIVE'){
           sp.Active__c = TRUE;
        }else {
           sp.Active__c = FALSE;
         }           
           updChildrecs.add(sp);
      }
      if(updChildrecs.size()>0){
          update updChildrecs;
      }
     
    
    }



}
}

Trigger
trigger SolutionParticipationCheckboxUpdate on Card_Program__c (after Update) {
     CardProgramsolparticipationcheck.CheckFieldstatus(trigger.new);
}
subodh chaturvedi 17subodh chaturvedi 17
Hi v varaprasad,
The Code is Working fine My bad I havn't Activate the trigger