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
James Gordon 10James Gordon 10 

"Can not select person account" error doesn't show in Sandboxes?

We have an existing org with person accounts enabled.  Recently, several unit tests have started failing with this error: 
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Can not select a person account: [AccountId] 
Stack Trace: Class.ContactTriggerTest.testContactLeadMerge_Success: line 18, column 1

Based on other threads, it seems that manipulating contacts is no longer allowed for person accounts.  We'll have to rework quite a bit of code.

My issue is that this error does not occur in sandboxes.  Is that expected?  We have created a brand-new dev sandbox, and the failing tests run fine.

Here's the unit test that works fine in the sandboxes, but not in production:
 
@isTest
private class ContactTriggerTest{
                 
    private static testMethod void testContactLeadMerge_Success() {
        Lead lead = new Lead(Lastname = 'testdata', Firstname = 'testdata1', Email = 'test@test.com');
        insert lead;
    
        //Inserting a person acct creates a contact. But it wouldn't fire the contact trigger
        //We'll leave this here as a general test that other triggers are firing fine.
        Account person = new Account(LastName = 'person', PersonEmail = 'test@test.com', OwnerId = Userinfo.getUserid());
        insert person;
        
        //Not sure of the purpose of below line?
        //update [SELECT Id FROM Contact];
    
        //Inserting a generic contact solely to fire the trigger
        Contact contact = new Contact(Lastname = 'testdata', Firstname = 'testdata1', Email = 'test@test.com', OwnerId = Userinfo.getUserid());
        insert contact;
        
        //System.assertEquals(2, [SELECT Id FROM Contact].size());
        System.assertEquals(0, [SELECT Id FROM Lead WHERE IsConverted = false].size());
    }
}

And here's the error:
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Can not select a person account: [AccountId] 
Stack Trace: Class.ContactTriggerTest.testContactLeadMerge_Success: line 18, column 1



Needless to say, it's very frustrating to adjust a class and upload for deployment, only to find that production has a specific error that the sandboxes don't.  Any advice on how to get this error to show in the sandbox, or why they are different?

Lastly, is there some way to get production working again by sidestepping this issue?  I've tried downgrading the API version of the class to 35, but no luck.  Thoughts?