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
amateur1amateur1 

tets case help

i have 3 queries in my controller i am able to cover the first query but not able to cover the contact and account queries please look at the queris and help how to cover them

 

 

 

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];

Abhay AroraAbhay Arora

Are your test runs succesfull if not can you put the test results .. here ?

amateur1amateur1

this is the test case i have writtena nd i am getting this error

 

invalid cross reference key

 

@istest
private class MyChecklist_All_Tc
{
static testMethod void validatetest1()
{
            Account a = new Account();
          
            a.BillingState = 'AL';
            a.name='ff';
            
            insert a;
            
            Contact c = new Contact();
           
            c.LastName = 'Contact';
            
            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;
         
            
      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, contactid=c.id,
                  timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com'); 
     
                 
                System.runAs(u) {
                 
           Account ac = new Account();
          a.name='sdsd';
             ac.BillingState = 'AL';
                     insert ac;
              
           
            Contact c1 = new Contact();
            c1.FirstName = 'Test';
            c1.LastName = 'Contact';
            c1.Email = 'contactemail@example.com';
           insert c1;
            Account ac1 = new Account();
             ac1.lastName = 'Test Account';
             ac1.BillingState = 'AL';
          
            insert ac1;
            
            opportunity o1 = new opportunity();
            o1.name='test';
            o1.StageName='prospecting';
            o1.CloseDate=date.valueof('2011-10-05');
            o1.Accountid=ac.id;
             insert o1; 
             MyChecklist__c m1=new MyChecklist__c();
             m1.name='Test';
             m1.Active__c=true;
             m1.Opportunity_Name__c=o1.id;
  
             ApexPages.currentPage().getParameters().put('id', m1.id); 
      
       MyChecklist_All  s1= new MyChecklist_All();
        
             
         }
       }
       
Abhay AroraAbhay Arora

On which line you are getting this error ?

amateur1amateur1
Abhay AroraAbhay Arora

Ok so i got your problem

 

PersonContactId is NULL in your case

 

1.) Create  test data for user, Contact and account with PersonContactId as id of contact you have created it will solve your problem


testMethod static void testWebSrvceAddNumbers()
{

    
    contact c=new contact();
    c.ts2__Lastname__c='dds';
    c.LastName='dds';
    c.accountid=a.id;
    c.CurrencyIsoCode='INR';
    insert c;
    

  account a =new account();

 a.PersonContactId=c.id;
    a.Preferred_Delivery_Address__c='Home';
    a.E_mail__c='arora.abhay@gmail.com';
    a.name='dsds';
    //a.ts2__Lastname__pc='dds';
    insert a;

    Opportunity objNewOpp = new Opportunity(Name ='Test',CloseDate=System.today(),Stage='Initial',A

ccountId =a.Id);
    insert objNewOpp;
    
    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, contactid=c.id,
                  timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com');
                  
      //  user u = [Select id,name ,LastName,FirstName,ContactId,Profile.Name from user where id=:UserInfo.getUserId()];
         system.runas(u)
         {
         
         account a1 =new account();
a1.Preferred_Delivery_Address__c='Home';
a1.E_mail__c='sandeep@tvarana.com';
a1.name='dds';
//a1.PersonContactId=c.id;
insert a1;
         
                

MyChecklist_All m=new MyChecklist_All();
   }

   

}
}


 


 

Above will solve your problem

amateur1amateur1

HI 

Personcontact id is not a writeable field this is the error i am getting what should i do now