+ Start a Discussion

Insert new Personal Acoount Record through APEX code results in cross reference access error

Hi, I have Person Account enabled in my organization. Also, I have two Record Types created for Person accounts.   While writing unit function, I have a need to create personal account using APEX code of one of the record types. Has anybody written APEX code for the same?  I have tried below code that I collected from the community threads, but gives me an error at last line:  Error message: System.DmlException: Update failed. First exception on row 0 with id 001Q0000004sztRIAQ; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: [].  Code: 
Contact newContact = new Contact( firstname= 'Bob', lastname='Test', email = 'bob@test.com');insert newContact;Account newAccount = new Account (name = 'Bob Test');insert newAccount;newContact.accountId = newAccount.Id;update newContact;//get the person account record typeRecordType personaccountrecordtype = [select Id, Name, SobjectType,         IsPersonType from RecordType where SobjectType='Account'         and IsPersonType=True and DeveloperName=’MyPersonalAccountRecordType’];Account[] convertAccounts = [Select id, RecordtypeId from Account where Id = :newAccount.Id ];convertAccounts[0].RecordTypeId = personaccountrecordtype.Id;convertAccounts[0].LastName = newContact.lastname;System.debug(Database.update(convertAccounts));
 Regards,Chandrakant M