Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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   
}
share|improve this question

2 Answers 2

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.

share|improve this answer
    
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 '13 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 '13 at 14:09
    
Getting the same error –  Eagerin Sf Jan 28 '13 at 14:23

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.)

share|improve this answer
    
added my Apex class can u check once –  Eagerin Sf Jan 28 '13 at 14:18
    
Have you given visibility to this record type to your profile?. I would double check that. –  PepeFloyd Jan 28 '13 at 15:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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