function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jose ZunigaJose Zuniga 

Inserting person accounts in test mode

Hello
    My envrionment is using personAccounts. While I was developing my code I was running a testing method so my code will insert a person account. To my surprise, under this testing process after the insert command was applied, the respective inserted record showed an ID but the isPersonAccont value was FALSE. not only that, the record type was giving a null value and this was properly assigned using the command

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


If I run the same code out of the testing environment then that record is properly inserted in the database with the mentioned fields isPersonAccount and RecordType assigned to their proper values  (true and 'person Account' respectively). Any solution to have this working as expected in testing environment?

Thanks
Jose

 

Best Answer chosen by Jose Zuniga
Ravi Dutt SharmaRavi Dutt Sharma
Hi Jose,

Are you talking about the test classes? In test class, if you are inserting the person account, you cannot use the same instance to check the fields of that record. You need to query back the inserted record to access its fields.

All Answers

Ravi Dutt SharmaRavi Dutt Sharma
Hi Jose,

Are you talking about the test classes? In test class, if you are inserting the person account, you cannot use the same instance to check the fields of that record. You need to query back the inserted record to access its fields.
This was selected as the best answer
Jose ZunigaJose Zuniga
The insert command -for the person account- is executed in a non testing class method which is the method I want to test. This problem arises everytime I call this method from a testing class. Here it is the code of the method:

private String handleAccount(boolean create, User u, Map<String, String> attributes) {
    
    RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account'];
    Account a;
    boolean newAccount = false;
      
    if(create) {   
    
          system.debug('In HanldeAccount, inserting a new account');
         
          a = new Account();
          newAccount = true;

    } else {     }

     if (attributes.containsKey('User.firstname')){
       a.firstName = attributes.get('User.firstname');
     }
     
   //  a.LastName
   
     if (attributes.containsKey('User.lastname')){
       a.LastName = attributes.get('User.lastname');
     } 
   
     a.RecordType = personAccountRecordType;
     system.debug('RecordType : ' + a.RecordType);
  
    if(newAccount) {
      
      try {      
             system.debug('Account to be inserted: ' + a);
             insert(a);
             system.debug('Account inserted: ' + a);
             system.debug('isPersonAccount = ' + a.IsPersonAccount);
             system.debug('Person Contact Id : ' + a.PersonContactId);
             system.debug('Recortype again: ' + a.recordtype);

           
      } catch (System.CalloutException e)
              {
                   System.debug('Error while trying to insert an account: ' + e);    
                  
              }
      
    } else {
      update(a);
    }
    return a.Id;
  }

-----------
Whenever this method is called fom a testing class -to be tested- the system.debug('isPersonAccount = ' + a.IsPersonAccount)  command  shows false, the system.debug('Recortype again: ' + a.recordtype)  shows null but the system.debug('Person Contact Id : ' + a.PersonContactId); shows an assigned ID. Out of the non testing class the mentioned commands show: true, 'Person Account' and a proper Id. value
Jose ZunigaJose Zuniga
Ravit, I added the next code just as you suggested:

system.debug([select isPersonAccount, PersonContactId  FROM account where ID =: a.Id]);

and I got:

09:21:17:762 USER_DEBUG [307]|DEBUG|Account inserted: Account:{FirstName=Jack, LastName=Sparrow, CTSC_Sponsored_Biostats_Hours__c=5, Id=0015500000Es6LiAAJ}
09:21:17:762 USER_DEBUG [308]|DEBUG|isPersonAccount = false
09:21:17:762 USER_DEBUG [309]|DEBUG|Person Contact Id : null
09:21:17:762 USER_DEBUG [310]|DEBUG|Recortype again: RecordType:{Id=0121a000000EPCjAAO}
09:21:17:800 USER_DEBUG [311]|DEBUG|(Account:{IsPersonAccount=true, PersonContactId=0035500000C27txAAB, Id=0015500000Es6LiAAJ})

So, you were right!!
Thanks
Jose