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
Vijay@sfdcVijay@sfdc 

Relation ship query issue

Hello Everyone,

could you provide me soluction for displaying relationship data, in the below code debug statement always displaying NULL( in line number 17)
@istest  
public class testdatafactory {
    public static List<account > accountRecords(Integer num) {
        List < account > AccList = new List < account > ();
        for (Integer i = 0; i < num; i++)
        {
            account Acc = new account ();
            Acc.name = 'Account Testdata '+ i;
            Acc.BillingCity = 'chennai';
            Acc.BillingCountry = 'India';
            Acc.BillingPostalCode = '6000003';
            Acc.BillingState = 'tamilnadu';
            Acc.BillingStreet = 'West street';
            AccList.add(Acc);
        }
        insert AccList;
        return AccList;
    }
    public static List<contact > contactRecords(Integer num,list<account> acc) {
        List < contact > AccList = new List < contact > ();
        for (Integer i = 0; i < num; i++)
        {
            contact c = new contact ();
             c.LastName= ' Last Name'+i;
            c.accountID=acc[i].id;
            AccList.add(c);
            
        }
        insert AccList;
        return AccList;
    }
}

@istest
public class TestClass {
  static testmethod void Unittest()
    {
         list<account> acc= testdatafactory.accountRecords(10);
        //list<account> acclist=[select name,BillingCity from account];
         list<contact> con= testdatafactory.contactRecords(10,acc);
        
         
        for(contact c:con)
         {
             system.debug('===>'+c.account.name);
         }
 }

Thanks
Vijay
Best Answer chosen by Vijay@sfdc
santanu boralsantanu boral
You can not access relationship data like that approach.

If you run the statement you will see that accountId will get displayed. So there are no such errors are occuring and methods of testdatafactory class are performing well. 
for(contact c:con)
         {
             system.debug(c.accountId  + '===>'+c.account.name);
         }

To display relationship data you need to perform separate SOQL query based on contacts. Hope it helps.

P.S. - Please mark this answer as closed if you are happy.
 

All Answers

Greg HGreg H
If null lists are being returned from your methods then the records are not being created. It may be due to a validation rule, missing required field, or something else completely. Using a try catch statement might make troubleshooting a litle easier.
try {
	insert AccList;
	return AccList;
} catch(Exception e) {
	System.Debug('Error: '+e.getMessage());
	return null;
}
santanu boralsantanu boral
You can not access relationship data like that approach.

If you run the statement you will see that accountId will get displayed. So there are no such errors are occuring and methods of testdatafactory class are performing well. 
for(contact c:con)
         {
             system.debug(c.accountId  + '===>'+c.account.name);
         }

To display relationship data you need to perform separate SOQL query based on contacts. Hope it helps.

P.S. - Please mark this answer as closed if you are happy.
 
This was selected as the best answer