+ Start a Discussion
salesforcefan19salesforcefan19 

Trigger to update a checkbox in a relatedlist

Hello,

          I have a requirement to write a trigger to update the checkbox on the relatedlist of an object.

 

Scenario

 

There are 2 objects here

 

1.master__c

2. child__c

 

There is a custom field on the child__c called "check__c". so if there are two child__c records on a master__c record. only one child__c record can be updated with the check__c as "true" at any given point of time, so, if we try to update the other child record as true, the check box on the first record should get unchecked.

 

Any help will be appreciated.

 

Thanks,

vamshi

Peter_sfdcPeter_sfdc
Can you share what you've tried so far?
LokendraLokendra

Check this out I hope this will help.

 

trigger toUpdateCheckBoxOnChildAndOneIsTrueAtaTime on Child__c (after insert,after update) {
  List<Child__c> childList= new List<Child__c>();
  for(Child__c c:trigger.new) {
      if(d.ChechBox__c==true) {
           dm = [Select id,name from Child__c where ChechBox__c=true];
      }
      for(Child__c c1:childList) {
           if(c1.id!=c.id) {
               c1.ChechBox__c=false;
               update c1;
           }
      }
  }
}