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
Tanner RussellTanner Russell 

Error while testing delete contacts

I am getting this error while trying to delete contacts in a test class
System.DmlException: Delete failed. First exception on row 0 with id 003Q0000018OY8CIAW; first error: INVALID_PERSON_ACCOUNT_OPERATION, cannot reference person contact: []

Deletion works fine if I run the code normally but in the test I get this, how can I work around this for testing purposes or should my query be changed to avoid person accounts?
delete[select id,name from Contact where name like '%test%'];
Best Answer chosen by Tanner Russell
Anjita MaheshwariAnjita Maheshwari
Hi Tanner,

Yes, you have to avoid person accounts in your query by using 'Account.isPersonAccount = false' clause. 

The reason behind this is, you can only modify the person contact but can not create or delete it. It will automatically be created and deleted with person account. You may refer #4 and #5 in document https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_guidelines_personaccounts.htm . 

Thanks,
Anjita

All Answers

Tarun J.Tarun J.
Hello Tanner,

You can filter person accounts by using, '.Account.isPersonAccount = false' in WHERE CLAUSE.

-Thanks,
TK

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Anjita MaheshwariAnjita Maheshwari
Hi Tanner,

Yes, you have to avoid person accounts in your query by using 'Account.isPersonAccount = false' clause. 

The reason behind this is, you can only modify the person contact but can not create or delete it. It will automatically be created and deleted with person account. You may refer #4 and #5 in document https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_guidelines_personaccounts.htm . 

Thanks,
Anjita
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Update your query like below
delete[select id,name from Contact where name like '%test%' and IsPersonAccount = false];

Let us know if this will help you