+ Start a Discussion
Mats ErikssonMats Eriksson 

Creating Person Account with Apex

I have created a before insert trigger on case that will query Account for the existence of a social security number and if there is no match, a person account is created.

 

I then update the case with AccountID and PersonContactID (for ContactID).

 

My problem is that while I can update the case with the AccountID, the PersonContactID is null (also IsPersonAccount is false at this point). When I inspect the new account afterwards, there is a PersonContactID and IsPersonAccount is true.

 

The code compiles fine and executes fine with no errors doing all it should do except setting the ContactId.

 

It seems to me that the person account creation is a two step rocket behind the scene. First the account is created, then an internal trigger fires which creates the "shadow contact" and updates the account with the necessary information like the PersonContactID.

 

Thus my attempt to update the case happens in beteween those two steps. Am I right?

 

What can I do to work around this fact?

 

/Mats

Cory CowgillCory Cowgill

I deal with Person Accounts all the time in the Wealth Management / Private Equity space as its a B2C business model.

 

I haven't run into this problem personally. However, I can offer a few suggestions:

 

1. After you insert the PersonAccount record, can you then turn around and query the PersonAccounts records you created instead of using the object in memory that you just created. Step A insert PersonAccount as you do today. Step B Turn around and Query for ContactID using the AccountId for the PersonAccount you just created?

 

2. If suggestion 1 doesn't work, you can create the Account as a Business Recordy Type, and Create the Contact. Then you can convert the Business Account into a PersonAccount. That will have all the records created, both the PersonAccount and its "Shadow" contact which sits behind the scenes.

Mats ErikssonMats Eriksson

Thank you for your answer,

 

I will try out your plan 1.

 

Plan 2 I have already tried but I ran into problems marrying the two together, something like "INVALID_FIELD_FOR_INSERT_UPDATE, Cannot specify any additional fields when marrying or separating a Person-Account []"

 

Best regards.

 

/Mats

Mats ErikssonMats Eriksson

Hi,

I have tried your alternative 1, but stumble on s syntax problem.

 

//Here I update the case object with account ID's, which work perfectly.
   List<Account> accIds=new List<Account>();
    for (Case caseObj:casesToUpdate) {
    	Account newAccount = SSNumToAccountMap.get(caseObj.SSNumber__c);
    	caseObj.AccountId = newAccount.id;
    	accIds.add(newAccount.id);
        caseObj.Subject= caseObj.Reason+' - '+caseObj.Fname__c+' '+caseObj.Lname__c;
    }

// Here I requery the account for fresh data
    Map<Id,Account> mAccs= new Map<Id,Account>([Select  Id, PersonContactId from Account where Id IN :accIds]);

// Now I attempt to update the case with the PersonContactID from the new query
    for (Case caseObj:Trigger.new) {
        if (caseObj.ContactId==null && !takenSSNumbers.contains(caseObj.SSNumber__c))
        Account newAcc = mAccs.get(caseObj.AccountId);
// this refuses to compile because of the line below with 'Save error: Variable does not exist: newAcc.PersonContactId'
		caseObj.ContactId = newAcc.PersonContactId;	
		system.debug('### PersonContactId?: '+newAcc.PersonContactId);		        
	}

 

In my world I have pulled AccountID's and PersonContactId's into the map mAccs which I later query by Key to obtain the PersonContactID. Obiously that is not happening.

 

What is wrong?

 

/Mats