+ 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 Khatri (TK)Tarun Khatri (TK)
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