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).