I want to make @TestSetup Method to make coverage for my trigger. I need to make two Person Account and one is related to another. My code like below:

//  Person Account Setup
String recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('PersonAccount').getRecordTypeId();
System.debug('recordTypeId: ' + recordTypeId);
Account parent1 = new Account();
parent1.RecordTypeId = recordTypeId;
parent1.LastName = '문';
parent1.FirstName = '지범';
parent1.PrimaryDoctor__c = '미정';
parent1.Source__c = '기타';
insert parent1;

parent1 = [SELECT Id, Name, LastName, FirstName, IsPersonAccount FROM Account WHERE LastName = '문' ORDER BY CreatedDate DESC LIMIT 1];

Account acct1 = new Account();
acct1.RecordTypeId = recordTypeId;
acct1.LastName = '김';
acct1.FirstName = '지호';
acct1.PrimaryDoctor__c = '미정';
acct1.Source__c = '고객소개';
insert acct1;

if(parent1.IsPersonAccount) {

    acct1 = [SELECT Id, Name, LastName, FirstName, IsPersonAccount, PersonContact.Account__c FROM Account WHERE LastName = '김' ORDER BY CreatedDate DESC LIMIT 1];
    System.debug('acct1: ' + acct1);

        acct1.PersonContact.Account__c = parent1.Id;
        update acct1;
        System.debug('acct1 is updated');

    acct1 = [SELECT Id, Name, LastName, FirstName, IsPersonAccount, PersonContact.Account__c, PersonContact.Account__r.LastName, PersonContact.Account__r.FirstName FROM Account WHERE LastName = '김' ORDER BY CreatedDate DESC LIMIT 1];
    System.debug('acct1 parent: ' + acct1.PersonContact.Account__r.LastName + acct1.PersonContact.Account__r.FirstName);

When I excute anonymose with my code, there is no error. but acct1.PersonContact.Account__r.LastName and acct1.PersonContact.Account__r.FirstName is null.

And the logs like below:

20:53:25.41 USER_DEBUG [3]|DEBUG|recordTypeId: 0120l000000XBdjAAG
20:53:26.74 USER_DEBUG [25]|DEBUG|acct1: Account:{Id=0010l00000s7VURAA2, Name=김 지호, LastName=김, FirstName=지호, IsPersonAccount=true, PersonContactId=0030l00000V3qNDAAZ}
20:53:26.74 USER_DEBUG [30]|DEBUG|acct1 is updated
20:53:26.74 USER_DEBUG [34]|DEBUG|acct1 parent: nullnull

I don't know why I can't make person account to be related to another one. Could you please let me know how can i solve this problem?

Thanks Regards, Changjoo Sohn.

1 Answer 1


Option 1

if(parent1.IsPersonAccount) {
    acct1 = [SELECT Id, IsPersonAccount, PersonContact.Account__c FROM Account WHERE Id = :acct1.Id];

        acct1.PersonContact.Account__c = parent1.Id;
        update acct1.PersonContact;

Option 2

if(parent1.IsPersonAccount) {
    acct1 = [SELECT Id, IsPersonAccount, PersonContactId FROM Account WHERE Id = :acct1.Id];

    if (acct1.IsPersonAccount) {
        update new Contact(Id = acct1.PersonContactId, Account__c = parent1.Id);

You must log in to answer this question.

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