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
shiv kumar 48shiv kumar 48 

Parent attribute not able to access in child soql in test class

The second debug of Test class always giving null can you please explain ?
 
---------------------Test classs-----------------------
@isTest
public class TestRelationship
{
 public static testMethod void test1Rel(){
            Account pr=new Account();
            pr.Name='TestParent';
            insert pr;
            Contact chd=new contact();
            chd.Account= pr;
            chd.lastname='Paru';
            insert chd;
           Contact con = [select Account.Name, lastname from Contact limit 1];
           System.debug('Contact Name........'+con.lastname);
           System.debug('Contact Account......'+con.Account.Name);
            Test.startTest();
            TestRelation.testRel();
            Test.stopTest();
          }
}

------------Apex class---------------------
public class TestRelation
{
  public static void testRel()
   {
   Contact con = [select Account.Name, lastname from Contact limit 1];
       System.debug('Contact Name'+con.lastname);
       System.debug('Contact Account'+con.Account);
       if(con.Account.Name != null)
       {
       System.debug('I am Here');
       integer i=0;
       integer j=0;
       }
   }

}



 
Best Answer chosen by shiv kumar 48
Abhishek BansalAbhishek Bansal
Hi Shiv,

Please change your test class with below code :
public class TestRelationship
{
	public static testMethod void test1Rel(){
		Account pr =new Account();
		pr.Name='TestParent';
		insert pr;
		
		Contact chd=new contact();
		chd.AccountId = pr.id;
		chd.lastname='Paru';
		insert chd;
   
		Contact con = [select Account.Name, lastname from Contact where id = :chd.id limit 1];
		System.debug('Contact Name........'+con.lastname);
		
		System.debug('Contact Account......'+con.Account.Name);
		Test.startTest();
		TestRelation.testRel();
		Test.stopTest();
	}
}

Let me know if you have any issue.

Thanks,
Abhishek

All Answers

Alexander TsitsuraAlexander Tsitsura
Hi shiv,

You need assign account to contact. You need use "chd.AccountId= pr.Id;" insetead of "chd.Account= pr;"

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex
Abhishek BansalAbhishek Bansal
Hi Shiv,

Please change your test class with below code :
public class TestRelationship
{
	public static testMethod void test1Rel(){
		Account pr =new Account();
		pr.Name='TestParent';
		insert pr;
		
		Contact chd=new contact();
		chd.AccountId = pr.id;
		chd.lastname='Paru';
		insert chd;
   
		Contact con = [select Account.Name, lastname from Contact where id = :chd.id limit 1];
		System.debug('Contact Name........'+con.lastname);
		
		System.debug('Contact Account......'+con.Account.Name);
		Test.startTest();
		TestRelation.testRel();
		Test.stopTest();
	}
}

Let me know if you have any issue.

Thanks,
Abhishek
This was selected as the best answer
shiv kumar 48shiv kumar 48
Thanks @Alexander and @Abhishek for your answer .