Tuesday, May 17, 2016

APEX: Creating Person Accounts in Salesforce using Apex

If your current org supports Person Accounts, you will need to let Salesforce know that you want to create Person Accounts and not Business Accounts.

First option is to query the RecordType object as follows:

List<RecordType> rType = [Select Id, Name From RecordType Where name = 'Person Account' 
and sobjectType = 'Account'];

This is pretty straightforward.  However, if your client decides to customize the Name for the record type (i.e. Person Accounts 2), the above query is not longer flexible.

Second option is to query the RecordType object using the IsPersonType field as follows:

List<RecordType> rType = [Select Id, Name, sobjectType, IsPersonType From RecordType 
Where IsPersonType = true and sobjectType = 'Account'];

Finally, you will need to create the Person Account as follows:

Account acct = new Account(); acct.RecordTypeId = rType .get(0).Id; acct.LastName = 'Test';


No comments:

Post a Comment