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
AISHIK BANERJEE 15AISHIK BANERJEE 15 

Getting this error. There's a problem saving this record. You might not have permission to edit it, or it might have been deleted or archived. Contact your administrator for help.

trigger preventAccDel on Account (before delete) {
    List<Id> accountIds=new List<Id>();
    Integer count=0;
    for(Account acc:Trigger.New){
        accountIds.add(acc.Id);
    }
    for(Account a:[SELECT Id, Name, (SELECT Id, AccountId FROM Contacts) FROM Account where Id IN :accountIds]){
        for(Contact con:a.Contacts){
            count=count+1;
        }
        if(count>=2){
            a.addError('This Account is associated with 2 or more Contact records. Hence cannot be deleted');
        }
        count=0;
    }
}
SwethaSwetha (Salesforce Developers) 
HI Aishik,
Does this similar post form past https://salesforce.stackexchange.com/questions/377049/salesforce-edit-field-on-before-delete help?

If this information helps, please mark the answer as best. Thank you
 
Shri RajShri Raj

This is an Apex trigger for the Account object in Salesforce, which is intended to prevent deletion of Accounts that have at least two associated Contacts. The trigger is executed "before delete" and checks each Account in the trigger context (i.e. the Accounts that are being deleted) to see if it has at least two associated Contacts. If it does, the trigger adds an error message to the Account, preventing it from being deleted. The error message is "This Account is associated with 2 or more Contact records. Hence cannot be deleted". The trigger is using a SOQL query in a loop to check the associations. This can cause performance issues if many records are being deleted at once.
AISHIK BANERJEE 15AISHIK BANERJEE 15

Yes, the analysis is correct but I am not able to understand why is the code not working. Even when I try to delete one account, I get the below error.

"There's a problem saving this record. You might not have permission to edit it, or it might have been deleted or archived. Contact your administrator for help."

Shri RajShri Raj
There could be multiple reasons for the code not working.
One possibility is that the trigger is not activated or not properly deployed to your Salesforce org.

Another possibility is that there is another trigger or validation rule that is conflicting with this trigger and preventing the deletion of the Account.

Also, make sure the user who is trying to delete the Account has the necessary permissions to delete the Accounts.

One more reason could be that the accounts you are trying to delete have child records other than contacts. In that case, you have to add the same error checking for that child records as well.


I would recommend checking these things and then trying to delete the Account again. If the problem persists, it would be helpful to see the full debug log for the deletion action to see what is causing the error