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

__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 ,
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 ,
You need to put actuall field and and change __c to __r.
ex: child has lookup to parent which is api name is looktoparent
[select looktoparent.Name from child];
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
@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 ?
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
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];
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:
Please update your test class
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:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
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