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
MarrisMarris 

How to create person account in Test class

Hi

 

    I had a query in my class stated like this

 

   User user = select conatctid form User where id=:userinfo.getUserId();

   Account acc = Select mailingcity,mailingstate from account where personconatctID=:user.contactId();

 

  For this I have to write a test class. I dont know how to create a dummy person account  and assign it to User for testing

 

I have to test by creating a new person account and user to test the above Query how can i do this? 

 

Need a help on this....

 

Thanks

Marris.

~Onkar~Onkar

Hi Marris.

 

This is the way you can create Person Account in SFDC test Class.

 

 

String RecTypeId= [select Id from RecordType where (Name='Person Account') and (SobjectType='Account')].Id;

 

Account Accnt = new Account(

  RecordTypeID=RecTypeId,

  FirstName=’Test FName’,

  LastName=’Test LName’,

  PersonMailingStreet=’test@yahoo.com’,

  PersonMailingPostalCode=’12345’,

  PersonMailingCity=’SFO’,

  PersonEmail=’test@yahoo.com’,

  PersonHomePhone=’1234567’,

  PersonMobilePhone=’12345678’ 

);

 

insert newAccount;

 

 

But as see your code seems you are using partner User.

 

So need more code for that create a partner user in test class use RunAs() method in test class after that may be you will cover your test class successfully.

 

I can help you more if you put your complete code here.

 

Thanks

Onkar Kumar

 

 

MarrisMarris

Hi onkar 

 

      Thanks for the help. Below is my code. In that code I am trying to cover saveShipping method.

 

public with sharing class RowclsCpshipaddress{

    public String MailingAddress1,Zipvalidate,MailingAddress2,MailingState,MailingCity,MailingZip,Address,State,City,Zip;
    public String getMailingAddress1() { return this.MailingAddress1; }
    public void setMailingAddress1(String el) { this.MailingAddress1 = el; }
    public String getMailingAddress2() { return this.MailingAddress2; }
    public void setMailingAddress2(String el) { this.MailingAddress2 = el; }
    public String getMailingState() { return this.MailingState; }
    public void setMailingState(String el) { this.MailingState = el; }
    public String getMailingCity() { return this.MailingCity; }
    public void setMailingCity(String el) { this.MailingCity = el; }
    public String getMailingZip() { return this.MailingZip; }
    public void setMailingZip(String el) { this.MailingZip = el; }
    //public Boolean savepersonalpanel { get; set; }
    public Boolean savebillpanel { get; set; }
    public Boolean isSaved { get; set; }
    
    public List<Account> listacc = new List<Account>(); 
    public Account objacc = new Account(); 
    public Account objupdacc = new Account(); 
    public Account objupdacc2 = new Account();
    public List<Account> objupdacc3 = new List<Account>();  
    private User user;
      
   public PageReference saveshipping(){
  User user = [select id,AccountId, ContactId, Name,usertype from User where        id=:UserInfo.getUserId()]; 
 objupdacc3 = [select Mailing_Zip_Code__c,Mailing_State__c,Mailing_City__c,Mailing_Address_2__c,Mailing_Address_1__c,Service_Zip_Code__c from Account where PersonContactId =:user.ContactId limit 1];
    try{ 
       if(objupdacc3.size()>0){
       if(MailingAddress1 == '' || MailingAddress2 =='' || MailingCity =='' || MailingState =='' || MailingZip ==''){
           ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Enter valid values in all fields.')); 
           return null;
       }
       if(objupdacc3[0].Service_Zip_Code__c != null){       
         if(objupdacc3[0].Service_Zip_Code__c != MailingZip){
           ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Your shipping Zip code is not match with your current Zip code.')); 
           return null;
         }        
         objupdacc3[0].Mailing_Address_1__c = MailingAddress1;
         objupdacc3[0].Mailing_Address_2__c = MailingAddress2;
         objupdacc3[0].Mailing_City__c = MailingCity;
         objupdacc3[0].Mailing_State__c = MailingState;
         objupdacc3[0].Mailing_Zip_Code__c = MailingZip;
         update objupdacc3;
         //PageReference redirect = new PageReference('/apex/ROWCpDashboard'); 
         //redirect.setRedirect(true); 
         //return redirect; 
         isSaved= True;
         savebillpanel = false;
         return null;
         }else{
         ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Enter valid Personal address Zip.')); 
         return null;
         }
      // return null;
       }
       }Catch(Exception ex){
         ApexPages.addMessages(ex); 
         return null;
       }*/
       return null;
      } 
      
   public RowclsCpshipaddress(){
      savebillpanel = True;
      issaved = false;   
   }

 

 

rakesh.mupirirakesh.mupiri
 
String recordTypeId  = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
        Account acc= new Account(
          RecordTypeID=recordTypeId ,
          FirstName='Test FName',
          LastName='Test LName',
          PersonMailingStreet='test@yahoo.com',
          PersonMailingPostalCode='12345',
          PersonMailingCity='SFO',
          PersonEmail='test@yahoo.com',
          PersonHomePhone='1234567',
          PersonMobilePhone='12345678' 
        );
 
        insert acc;