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 45Shiv Kumar 45 

__r not working in test class

In the class i am accessing the parent record attribute with __r in the select query in child query like below .
Childrecord__c child = [ select Parentobj__r.Account__c  from childobject__c where name ='Test' limit 1 ];
if(child.Parentobj__r.Account__c != null)
{
do something();
}
In test class i have created parent and child record ,
but value of Parentobj__r.Account__c  always giving  null value while debug please help here ,
 
Ashish Dev 3Ashish Dev 3
Could you tell Parentobj__c is the field name of lookup/masterdetail relation pointing to?
You need to put actuall field and and change __c to __r.
Mohammed AzarudeenMohammed Azarudeen
Make sure your lookup(Master object) field name on child object is Parentobj. If not then use lookup field apiname.fieldname in query
ex: child has lookup to parent which is api name is looktoparent
  [select looktoparent.Name from child];
ManojjenaManojjena
Hi Shiv,
In test class query is not required I think you need to create record and make relationship and if reocrd you will create then query in class will automatically execute .

Suppose
Parent pr=new Parent();
pr.Name=TestParent;
insert parent;
Child chd=new Child();
chd.Parent__c=pr.Id;
Insert pr;
Like above if you will create and your record will successfull insert then you class code will cover automatically with teh test data .
Check below link it will help to write test class .

http://manojjena20.blogspot.in/2015/06/tips-and-tricks-for-test-class.html
I think this will help !!
Thanks
Manoj
Shiv Kumar 45Shiv Kumar 45
Thanks all for the reply :
@Ashish / Azar : yes i do have a field Parentobj__c on the child object . Its a look up relation .
@Manoj : Yes i created the objects like you said but Parentobj__r.Name  is still giving null .
Is that its not working because these are not committed to Database as they are created in test class  and relationship (__r)  not working in query ?


 
shiv kumar 48shiv kumar 48
Here is my Actual class :
Here is my Actual class :

public class TestRelation
{

   public static void testRel()
   {

       Contact con = [select Account.Name , id from Contact limit 1];
       if(con.Account.Name != null)
       {
       System.debug('I am Here');
       integer i=0;
       integer j=0;
       }
   }



}

------------------------------test class---------------------------


?@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;
            TestRelation.testRel();

     }



}


When i run the test class :
The code inside the If block not executing
WeiweiWeiwei
I read a useful tipps here.
https://salesforce.stackexchange.com/questions/1885/why-is-my-reference-field-returning-null-my-test-code
please make a selection like this after the statement: insert chd;
Contact chd = [select Account.Name , id from Contact limit 1];
Prakhar Saxena 19Prakhar Saxena 19
Hi,

You need to add @isTest(SeeAllData=true) annotation on your test class so that it can access records in your organization and return results in SOQL query.

Secondly, you need to add a where clause check to ensure Account.Name is not null else it won't cover the if block and lastly no need to insert Account or Contact in the test method.

Since the testRel method is not expecting any arguments, you can directly call the method from the test class.

Below is the code for TestRelation and TestRelationship with 100% code coverage:
 
public class TestRelation{
    public static void testRel(){
        
        Contact con = [select Account.Name , id from Contact WHERE Account.Name!=NULL limit 1];
        if(con.Account.Name != null){
            System.debug('I am Here');
            integer i=0;
            integer j=0;
        }
    }
}


@isTest(SeeAllData=true)
public class TestRelationship{
   public static testMethod void test1Rel(){
            TestRelation.testRel();
     }
}

 
Sanjay Bhati 95Sanjay Bhati 95
Hi Shiv,

Please update your test class
@isTest
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;
            TestRelation.testRel();

     }
}

 
Ajay K DubediAjay K Dubedi
Hi Shiv,
 
In test class we create run time record only.So,we didn't query in test class.I think you need to create record and make relationship and if reocrd you will create then query in class will automatically execute .
Test Class:
@IsTest
public class TestRelationship{
@IsTest
private static void testRelation(){
Account__c   acc=new Account__c  ();
acc.Name='ParentAccount';
insert acc;
Parentobj__r pr=new Parentobj__r();
pr.Name=TestParent;
pr.Account__c=acc.Id;
insert pr;
Childrecord__c  chd=new Childrecord__c ();
chd.Parentobj__r=pr.Id;
Childrecord__c.Name='child';
Insert pr;
}
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
 
ChrisludoviceChrisludovice
I don't see the use of __r in queiring the test record, just create 2 records(parent and child) and established its relationship to be covered by your test class.

E.g.
Parent p=new Parent();
p.Name ='parent';
insert p;

Child c=new Child();
c.p__c=p.Id;
c.Name = 'child';
Insert c;

"__r" is used for retrieving field values from the object's related another object when those objects have relationship via Lookup field.

Thanks.

Regards,
Chris