2

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   
}

3 Answers 3

4

When working with Person Accounts you do not need to set the Contact separately; that is all handled automatically by Salesforce. Just create a Person Account and use that for testing:

static testmethod void invoicetest(){

   RecordType myRecordType = [select id from RecordType where SobjectType='Account' AND IsPersonType=True limit 1];

   Account acc = new Account(
     FirstName='test',
     LastName='Acc',
     Recordtypeid=myRecordType.Id);
   insert acc;
}

Edit: Modified SOQL for retrieving RecordType.

Update

Your Apex class is dealing with Contacts, your test class is trying to assign Contacts to Accounts, but your question is relating to Person Accounts. I believe both your Apex class and the tests should only be creating Accounts with your Person Account Record Type.

3
  • If I don't insert contact then I'm getting error as System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName]
    – Eagerin Sf
    Jan 28, 2013 at 14:00
  • Your code specifically mentions setting the LastName field, though. This is pretty much exactly what I use in my unit tests, too.
    – Mike Chale
    Jan 28, 2013 at 14:09
  • Getting the same error
    – Eagerin Sf
    Jan 28, 2013 at 14:23
1

Try creating it like this instead:

string strrecord = [select id from RecordType where SobjectType='Account' AND IsPersonType=True limit 1].id;

Account Acc = new Account(
  RecordTypeID=strrecord,
  FirstName='jane',
  LastName='doe');

insert Acc;

Update

After re-reading the Person Account Tips and Cheatsheet I noticed it specifically says that Person Accounts do not have a Contacts related list (Usage Notes, pages 2.)

2
  • added my Apex class can u check once
    – Eagerin Sf
    Jan 28, 2013 at 14:18
  • Have you given visibility to this record type to your profile?. I would double check that.
    – PepeFloyd
    Jan 28, 2013 at 15:12
0

In the newer APIs, the Contact is completely read-only if it is attached to a Person Account. In fact, the Contact record is only a "convenience" for the underlying data model. So in your test you don't need to create and insert a separate Contact and try and bind it to the Account. Insert the test Account and the test Contact will just be there.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .