/
Create a new Person Account
Create a new Person Account
When Person Accounts are enabled in Salesforce, these operate by combining both an Account and a related Contact record. Salesforce will automatically generate the Contact record if you set the LastName field on the Account and provide the correct RecordTypeId.
Account account = new Account();
account.InternalFields["FirstName"] = "John";
account.InternalFields["LastName"] = "Smith";
// The RecordTypeId can be set directly to a known Id, or...
//account.RecordTypeId = "<PersonAccountRecordTypeId>";
SalesforceSession salesforceSession = new SalesforceSession(new LoginDetails("username@example.com", "salesforcePassword"));
AccountService accountService = new AccountService(salesforceSession);
// ... the correct RecordTypeId can be retrieved via the AccountService.
var personAccountRecordType = accountService.RecordTypeInfosById()?
.Values.FirstOrDefault(rti => rti.developerName.Equals("PersonAccount"));
if (personAccountRecordType != null)
{
account.RecordTypeId = personAccountRecordType.recordTypeId;
var saveResult = accountService.Insert(account);
string newAccountId = null;
if (saveResult.success)
{
newAccountId = account.Id;
}
Assert.IsTrue(accountService.ValidEntityId(newAccountId));
} |