4

In Case Object there is a "special" lookup field to Account, with the API Name AccountId and Label Account Id (btw in the UI we see Account Name as the field's label which is the name we also see in the page).

When trying to delete an Account with a related Case, I get the error message

our attempt to delete Test Account 01 could not be completed because it is associated with the following cases.: 00001015.

If I log in to Salesforce Workbench and navigate to

Standard & Custom Objects > Account > Child Relationships

I can see that Case.AccountId has restrictedDelete: true which is perfectly logical considering the behavior described above. An Account cannot be deleted if there is a related Case against that Account.

However if I run the following code in Execute Anonymous Window on Developer console I get false:

Schema.DescribeFieldResult dfr = Schema.SObjectType.Case.Fields.AccountId;
//result below false
System.debug('dfr.isRestrictedDelete: ' + dfr.isRestrictedDelete()); 

This does not make sense since according to the reference guide isRestrictedDelete() should return true if the parent object can't be deleted because it is referenced by a child object, false otherwise. Also it is the opposite of the real behavior and what Salesforce Workbench shows.

A similar behavior is seen with the field AccountId of the Opportunity Object. When an Account is deleted all children opportunities get deleted as well. In Salesforce Workbench in Account child relationships, Opportunity.AccountId has isCascadeDelete: true.

However when trying to get that info through dynamic Apex I get false...

Schema.DescribeFieldResult dfr = Schema.SObjectType.Opportunity.Fields.AccountId;
//result below false
System.debug('dfr.isCascadeDelete: ' + dfr.isCascadeDelete());

When trying with Custom objects (e.g. custom Parent, Child with a lookup and checking Don't allow deletion of the lookup record that's part of a lookup relationship. I get isRestrictedDelete() as true..

Any idea why this is happening? Am I missing something? Is it because of the "special" nature of the relationship between Account and Contacts and Opportunities through the AccountId field(a lookup but with elements of a Master-Detail)?. Even if so how does Salesforce Workbench get the correct values? (I was under the impression they were using dynamic apex).

1 Answer 1

4

Yes, what you're experiencing is a result of standard built-in functionality, these "special" relationships. Note that you're allowed to delete an Account even if you have Cases and/or Opportunities attached to them, so long as there are no Cases or Opportunities with IsClosed equal to false. This is why Restricted Delete appears as "false," despite situations where the contrary is true.

I don't know of a single place in the documentation where you can find all of these special relationships, but there are not many of them. Just be aware that when working with core CRM objects (I believe the entire list is Account, Contact, Case, Opportunity, Quote, Contract, and Order), there are special rules about what you can do with them, so you may need to read the documentation and/or experiment to find any special restrictions.

Edit: I should have mentioned; the REST/SOAP API and Apex have different "rules" for the same objects. You may find that you can delete an object in Workbench, but you can't in Apex. These differences have to do with the nature of how Apex runs; some values are calculated at the beginning of a transaction, and modifying those values at runtime could break expectations, so they're not allowed.

1
  • Makes sense and it explains why the result of DescribeFieldResult.isRestrictedDelete() is false. However I tested trying to delete the account after putting the only existing related case to Status = 'Closed' (checked that isClosed = true) and still couldn't delete the Account. I tried deleting the Account through UI, Salesforce Inspector, Salesforce Workbench, and Apex and got the same error message as before.
    – SFAxl
    Jan 7, 2022 at 19:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .