3

Code

trigger CreatePerson on Contact (before insert) {

   RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account'];


   Map<Contact, Account> accounts = new Map<Contact, Account>();
   for(Contact record: Trigger.new) {
       if(record.AccountId == null) {
           Account newPersonAccount = new Account();
           newPersonAccount.FirstName = record.FirstName;
           newPersonAccount.LastName = record.LastName;
           newPersonAccount.RecordType = personAccountRecordType;

           accounts.put(record, newPersonAccount);
       }
   }

   insert accounts.values();

   for(Contact record: Trigger.new) {
       if(record.AccountId == null) {
           record.AccountId = accounts.get(record).Id;
       }
   }
}

Error

Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Can not select a person account".

How can I associate the recently created Person account with the Contact?

6

It seems like you want to convert a new Contact that is being inserted into a Person Account.

Have a look at the Convert existing account-contact pairs to person accounts steps in the Implementing Person Accounts documentation.

Convert existing account-contact pairs to person accounts

  1. Create one-to-one relationships between account and contact records. In other words, remove all contacts on the accounts except for the contact that will be converted with the account.
  2. Identify the accounts that have a different owner than the associated contact. Change the ownership of either record so both are owned by the same user.
  3. If your organization uses multiple currencies, identify the accounts that have a different currency than the associated contact. Change the currency of either record so both have the same currency.
  4. Make sure the Parent Account field is blank on all accounts that will be converted. Also, make sure none of the accounts are the parent account of another account.
  5. Make sure the Reports To field is blank on all contacts that will be converted. Also, make sure none of the contacts are in the Reports To field of another contact. 9 Implementing Person Accounts Recommended Implementation Procedures
  6. As much as reasonably possible, edit the accounts so they contain all desired field values.
  7. Using the SOAP API, update the record types of the chosen accounts to convert them to the desired person account record type. We recommend running a test conversion in your sandbox.

Now your steps will be a bit different. You will need to insert the Account and then link the Contact to it using the AccountId field. Then after the account has been inserted and the Contact linked to it you can change the accounts record type to indicate it is a Person Account.

Coming back to your trigger. I believe this will need to be an after update trigger so that the Contact can be inserted and then referenced back to the newly created Account. Then you can change the Accounts record Type.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.