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
VSK98VSK98 

Update child records

Trigger Updatechildrec on Account (after update )

List <Account > lst = new list<account>()
If (trigger. Isafter&&trigger.Isupdate){

For(account a: trigger.new)
If(a.id!=null && a. Check box == false)
lst. Add(a)
}

For(contact c: [Select I'd, accountid,checkbox from contact where accountid =:lst]){

C. Checkbox = false
Update c // we can list also to update...



}

Hello All, 
 I have to update the contact records when the account checkbox is false...Above code will work for the same? 
Regards,
VSK98
bretondevbretondev
Trigger Updatechildrec on Account (after update )

List <Id>lst = new list<account>()

If (trigger.Isafter&&trigger.Isupdate){

For(account a: trigger.new)
	If(a.Check box == false)
	lst.Add(a.id)
}

List<COntact> lstCont = [Select Id, accountid, checkbox from contact where accountid IN :lst];

For(contact c: lstCont ){
	C.Checkbox = false

}

Update lstCont  // we can list also to update...



}

 
bretondevbretondev
I made a mistake at line 3 , please correct :
List <Id>lst = new list<Id>();
Duke_SfdcDuke_Sfdc
Hi VSK98,

You can do this by using a Process Builder. And you can also write an Apex Trigger for the same. (See the Apex Trigger code below.)

trigger updateChildRecords on Account (After Insert,After update) {
    if(trigger.isAfter && ( trigger.isUpdate || trigger.isInsert) ){
    
        list<account> accList=(trigger.isInsert || trigger.isUpdate)?trigger.new:trigger.old;
        set<string> accountIdSet=new set<string>();
        
        for(account ac:accList){
            if(ac.checkbox__c==false)
                accountIdSet.add(ac.Id);
        }
        
        list<contact> conList=new list<contact>();
        for(contact con:[select id,accountId from contact where accountId in:accountIdSet]){
            con.checkBox__c=false;
            conList.add(con);
        }
        
        if(conList.size()>0)
            dataBase.update(conList);
    }
}

If this will fulfill your requirement, please mark this question as solved.

Thanks,
Duke_Sfdc