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
rameshramesh 

to Prevent the Deletion of the Related Contacts upon removing an Account Record. using flows

VinayVinay (Salesforce Developers) 

Hi Saki,

This might not be possible if you delete account, contacts would be deleted since they have parent child relationship.

Check below code snippet to have validation on account cannot be deleted if it has related contacts 
 

trigger DeleteAccount on Account (before delete)  
{  
    List<Account> accList = new List<Account>();  
    Set<id> accIdSet = new Set<id>();  
    for(Account acc : Trigger.old)  
    {  
        accIdSet.add(acc.id);  
    }  

    Map<Id, Account> accts = new Map<Id, Account>([Select Id, (Select Id from contacts) from Account where id in :accIdSet]);

    for(Account acc : Trigger.old)
    {
        if(accts.get(acc.id).contacts.size()>0)
        {
            acc.adderror('Account cannot be deleted');
            }
        }                                       

}

Please mark as Best Answer if above information was helpful.

Thanks,
rameshramesh
It's possible through triggers i already done but I want same thing using flows.
rameshramesh
Trigger code below working but I want in flows trigger PreventContactsTrigger on Account (before delete) { if(Trigger.isDelete && Trigger.isBefore) { // Get the Related Contacts for the Deleting Accounts.. List lstContacts = [Select id, firstname, lastname, accountid from Contact Where AccountID IN : Trigger.OldMap.KeySet()]; if(! lstContacts.isEmpty()) { for(Contact con : lstContacts) { con.AccountId = null; } update lstContacts; } } }