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
Mahesh Babu 187Mahesh Babu 187 

Trigger to update child record's checkbox field value on parent record deletion

Hi Team,

How to update child record's checkbox field value on parent record deletion using Lightning flow or trigger?
Best Answer chosen by Mahesh Babu 187
AnudeepAnudeep (Salesforce Developers) 
Hi Mahesh, 

This is just a sample code. Please modify as per your needs
 
trigger updateContactCheckbox on Account(after delete) {
    List<Id> AccountIds = new List<Id>(); 
    for(Account acc: Trigger.old) {
        AccountIds.add(acc.Id); 
    }

    List<Contact> contList = new List<Contact>(); 
    if(AccountIds.size()>0) {
        for(Contact c: [Select Id, Checkbox__c from Contact where AccountId IN:AccountIds]) {
           c.Checkbox__c ='your_value'; 
           contList.add(c); 
    }

    }
    update contList;
}

Let me know if it helps

Anudeep
 

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Mahesh,

Greetings!

You can create the Process builder and check for the criteria that whenever the lookup field is blank then update the checkbox=true.Instead of checking for deleted record since it is tricky to query or work on the deleted records.

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
AnudeepAnudeep (Salesforce Developers) 
Hi Mahesh, 

This is just a sample code. Please modify as per your needs
 
trigger updateContactCheckbox on Account(after delete) {
    List<Id> AccountIds = new List<Id>(); 
    for(Account acc: Trigger.old) {
        AccountIds.add(acc.Id); 
    }

    List<Contact> contList = new List<Contact>(); 
    if(AccountIds.size()>0) {
        for(Contact c: [Select Id, Checkbox__c from Contact where AccountId IN:AccountIds]) {
           c.Checkbox__c ='your_value'; 
           contList.add(c); 
    }

    }
    update contList;
}

Let me know if it helps

Anudeep
 
This was selected as the best answer