My test class for inserting contact is
static testmethod void invoicetest(){
string strrecord = [select id from RecordType where SobjectType='Account' AND IsPersonType=True limit 1].id;
system.debug('strrrrr'+strrecord);
Account acc = new Account(firstName='test',LastName='Acc',Recordtypeid=strrecord);
insert acc;
contact con = new contact(firstname='Test',LastName='Contact',Salutation='Mr',Accountid=acc.id);
insert con;
Then I'm getting error as System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Can not select a person account: [AccountId]
How to come out of this!!!!!!!!!
My Apex Class is
conlist = new contact[0];
con.lastname = billing.billinglastname;
con.firstname = billing.billinglastname;
conlist.add(con);
if(con.lastname != null){
insert conlist; //getting error here
}
Best Answer
When working with
Person Accounts
you do not need to set theContact
separately; that is all handled automatically by Salesforce. Just create aPerson Account
and use that for testing:Edit: Modified SOQL for retrieving RecordType.
Update
Your Apex class is dealing with
Contacts
, your test class is trying to assignContacts
toAccounts
, but your question is relating toPerson Accounts
. I believe both your Apex class and the tests should only be creatingAccounts
with yourPerson Account
Record Type.