You need to sign in to do that
Don't have an account?

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; } } }
Please change your test class with below code :
Let me know if you have any issue.
Thanks,
Abhishek
All Answers
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
Please change your test class with below code :
Let me know if you have any issue.
Thanks,
Abhishek