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
ran67ran67 

Testcase help On person account

Hi

How To intialize the Person account in a  test  case ......

 

Thanks in advances

Best Answer chosen by Admin (Salesforce Developers) 
DoomsdayDoomsday

"In this case you need create new user and set ContactId field" for new user.

Try this:

Account a = new Account();
a.LastName = 'Test Account';
insert a;

Opportunity o = new Opportunity();
o.name='test';
o.StageName='prospecting';
o.CloseDate=date.valueof('2011-10-05');
o.Accountid=a.id;
insert o;

Contact c = [SELECT Id FROM Contact WHERE AccountId=:a.Id];

Profile p = [select id from profile where name='Customer Portal Manager Custom'];
User u = new User(alias = 'standt', email='standarduser@testorg.com',
emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com', ContactId=:c.Id);

MyChecklist__c m1=new MyChecklist__c();
m1.name='Test1';
m1.Opportunity_Name__c=o.id;
m1.Active__c = 'true';
m1.Approval_Status__c = 'Accepted';
insert m1;

MyChecklist__c m2=new MyChecklist__c();
m2.name='Test2';
m2.Opportunity_Name__c=o.id;
m2.Active__c = 'true';
m2.Approval_Status__c = 'Refused';
insert m2;

//there must be MyChecklist__c with
//Approval_Status__c != 'Refused' and
//Approval_Status__c != 'Accepted'

System.runAs(u) {
 MyChecklist_All s1= new MyChecklist_All();
}

 

All Answers

DoomsdayDoomsday

Hi.

InternalServerErrorIsPersonAccount field is Read only.

ran67, to create Person Account you need specify the field LastName instead of Name field.
"When a person account is created (or an existing business account is changed to a person account), a corresponding contact record is also created." (Salesforce)

ran67ran67

hi still am not able to cover the code .......

when am intializing the lastname also

............please hellp me to cover the code 

i am sending my controller

controller:

public class MyChecklist_All
{

public Opportunity[] opp ;
public MyChecklist__c[] chklist{get;set;}
public MyChecklist__c[] chklistapprove{get;set;}
public MyChecklist__c[] chklistrefuse{get;set;}
public MyChecklist__c[] chkrefused{get;set;}
Set<Id> oppid = new Set<Id>();
Public Contact Con {get; set;}
Public Account Ac {get; set;}

public user u{get;set;}
public MyChecklist_All()
{

u = [Select id,name ,LastName,FirstName,ContactId,Profile.Name from user where id=:UserInfo.getUserId()];
Con=[select id,lastname from Contact where id =: u.ContactId];
Ac = [select id,lastname from Account where PersonContactId =: con.Id];
opp=[select id from Opportunity where Accountid=:ac.id];
for(Opportunity aa: opp)
{
oppid.add(aa.id);
}
chklist=[select id,Name,Product_Checklist__r.name,Approved_On__c,Received__c,Received_On__c,Specification__c,Received_Document_Remarks__c,Approval_Status__c,Status1__c from MyChecklist__c where Opportunity_Name__c IN: oppid
and Active__c=true];
chklistapprove=[select id,Name,Product_Checklist__r.name,Approved_On__c,Received__c,Received_On__c,Specification__c,Received_Document_Remarks__c,Approval_Status__c,Status1__c from MyChecklist__c where Opportunity_Name__c IN: oppid
and Approval_Status__c='Accepted'];
chklistrefuse=[select id,Name,Product_Checklist__r.name,Received__c,Approved_On__c,Received_On__c,Specification__c,Received_Document_Remarks__c,Approval_Status__c,Status1__c from MyChecklist__c where Opportunity_Name__c IN: oppid
and Approval_Status__c != 'Refused' and Approval_Status__c != 'Accepted'];
chkrefused=[select id,Name,Product_Checklist__r.name,Received__c,Approved_On__c,Received_On__c,Specification__c,Received_Document_Remarks__c,Approval_Status__c,Status1__c from MyChecklist__c where Opportunity_Name__c IN: oppid
and Approval_Status__c = 'Refused'];
}
}

please help me to cover the code ...........thanks in advance

DoomsdayDoomsday

"Con=[select id,lastname from Contact where id =: u.ContactId];"
In this case you need create new user and set ContactId field. And see please System.runAs method using

ran67ran67

hi

I have been created  the new user . but still am not able to cover the code.........

I am send my testcase

@istest
private class MyChecklist_All_Tc
{
static testMethod void validatetest1()
{


Profile p = [select id from profile where name='Customer Portal Manager Custom'];
User u = new User(alias = 'standt', email='standarduser@testorg.com',
emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com');
System.runAs(u) {
Account a = new Account();
a.Name = 'Test Account';
a.BillingState = 'AL';
insert a;

Contact c = new Contact();
c.FirstName = 'Test';
c.LastName = 'Contact';
c.accountid=a.id;
c.Email = 'contactemail@example.com';
insert c;

 

opportunity o = new opportunity();
o.name='test';
o.StageName='prospecting';
o.CloseDate=date.valueof('2011-10-05');
o.Accountid=a.id;
insert o;


MyChecklist__c m1=new MyChecklist__c();
m1.name='Test';
m1.Opportunity_Name__c=o.id;
insert m1;

MyChecklist_All s1= new MyChecklist_All();

}
}
}


DoomsdayDoomsday

"In this case you need create new user and set ContactId field" for new user.

Try this:

Account a = new Account();
a.LastName = 'Test Account';
insert a;

Opportunity o = new Opportunity();
o.name='test';
o.StageName='prospecting';
o.CloseDate=date.valueof('2011-10-05');
o.Accountid=a.id;
insert o;

Contact c = [SELECT Id FROM Contact WHERE AccountId=:a.Id];

Profile p = [select id from profile where name='Customer Portal Manager Custom'];
User u = new User(alias = 'standt', email='standarduser@testorg.com',
emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com', ContactId=:c.Id);

MyChecklist__c m1=new MyChecklist__c();
m1.name='Test1';
m1.Opportunity_Name__c=o.id;
m1.Active__c = 'true';
m1.Approval_Status__c = 'Accepted';
insert m1;

MyChecklist__c m2=new MyChecklist__c();
m2.name='Test2';
m2.Opportunity_Name__c=o.id;
m2.Active__c = 'true';
m2.Approval_Status__c = 'Refused';
insert m2;

//there must be MyChecklist__c with
//Approval_Status__c != 'Refused' and
//Approval_Status__c != 'Accepted'

System.runAs(u) {
 MyChecklist_All s1= new MyChecklist_All();
}

 

This was selected as the best answer