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
mh@reedmh@reed 

Testing Visualforce page that relies on object relationships

I have a Vf page that is based on a custom object which we'll call CustomQuote.  CustomQuote holds fields which we'll call BillToContact and SoldToContact which are a lookup reference to a standard contact object.  My controller code can examine aspects of the contact through the lookup relationship, e.g  CustomQuote.BillToContact__r.FirstName and in fact does this to validate that the appropriate data has been entered.  The page displays the value from this relationship without any problems.

 

My problem begins when writing the test class for the controller.  I create a CustomQuote and pass it through to the controller in the usual fashion,  i.e.

 

pageRef = Page.MyCustomQuotePage;

MyController controller;

 

Test.setCurrentPage(pageRef);

controller = new MyController(new ApexPages.StandardController(CustomQuote));

 

If I debug the CustomQuote object from the controller,  the CustomQuote is fully populated and contains a contact id for the bill-to and sold-to fields.  However,  when the validation piece runs (is CustomQuote.BillToContact__r.FirstName populated?) it always returns false because the relationship pulls back a null value.  I've coded a small select statement to pull back the contact details and they are in the database.

 

So,  my problem is:  a relationship such as CustomQuote.BillToContact__r.FirstName works in practice,  but,  cannot be tested because under those circumstances it does not.  Am I doing something wrong and,  if not,  how are people getting around it other than just ignoring this useful relationship functionality?

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

The problem is that specifying an id doesn't cause anything to happen to populate the relationship.

 

Off the top of my head, I think you could do this by inserting the custom quote into the database and then retrieving it via soql and populating the relationship.

 

Something like:

 

 

CustomQuote__c cq=new CustomQuote(Name='Test', 
              BillToContact__c=<id>, 
              SoldToContact__c=<id>);
insert cq;

CustomQuote__c relationshipCQ=[select id, name, BillToContact__c, BillToContact__r.FirstName, SoldToContact__c, SoldToContact__r.Firstname);

...


controller = new MyController(new ApexPages.StandardController(relationshipCQ));

 

 

All Answers

bob_buzzardbob_buzzard

The problem is that specifying an id doesn't cause anything to happen to populate the relationship.

 

Off the top of my head, I think you could do this by inserting the custom quote into the database and then retrieving it via soql and populating the relationship.

 

Something like:

 

 

CustomQuote__c cq=new CustomQuote(Name='Test', 
              BillToContact__c=<id>, 
              SoldToContact__c=<id>);
insert cq;

CustomQuote__c relationshipCQ=[select id, name, BillToContact__c, BillToContact__r.FirstName, SoldToContact__c, SoldToContact__r.Firstname);

...


controller = new MyController(new ApexPages.StandardController(relationshipCQ));

 

 

This was selected as the best answer
mh@reedmh@reed

Yes of course!  Fabulously simple really,  thanks for taking the time to answer.