4

I'm pretty new to SalesForce and my question should be very simple.

I'm creating an extension for a standard controller of a custom object called Bill_Account_c. This object has a master detail field with another custom object called Bill_c.

In the constructor, I want to get the Bill__c associated to the Bill_Account__c.

Here's what I've tried in apex:

public newConstructor(ApexPages.StandardController standardController) {
    controller = standardController;
    billAccount = (Bill_Account__c)controller.getRecord();

Bill__c bill = [SELECT Id, Bill_Field1__c, Bill_Field2__c FROM Bill__c WHERE Id = :billAccount.Bill__c];

}

but I get this error:

SObject row was retrieved via SOQL without querying the requested field: Bill_Account__c.Bill__c

What am I doing wrong? Sorry if this is a silly question.

  • There are no silly questions :) – Phil Hawthorn Jan 16 '14 at 15:08
1

You would need to re-query the record, so instead of what you have you would need to do this:

public newConstructor(ApexPages.StandardController standardController) {
    controller = standardController;
    billAccount = (Bill_Account__c)controller.getRecord();

    billAccount = [ Select Bill__c Where Id = billAccount.Id ];

Bill__c bill = [SELECT Id, Bill_Field1__c, Bill_Field2__c FROM Bill__c WHERE Id = :billAccount.Bill__c];

}

I think if Bill__c is not used in your VF page, you have to physically re-query it. The other solution would be to make it a hidden field in your VF page if it doesn't need to be displayed.

EDIT - or, as Mike points out an even better solution seems to be to use controller.addFields(List<String> fieldNames) thus saving yourself a SOQL query

  • 1
    Or use controller.addFields(List<string> fieldNames) - salesforce.com/us/developer/docs/pages/Content/… – Mike Chale Jan 16 '14 at 15:15
  • So when you do that, you don't need to re-query and getRecord() does the magic? Seems a better solution – Phil Hawthorn Jan 16 '14 at 15:18
  • Correct; you call addFields before getting the record and it adds those fields to the record. – Mike Chale Jan 16 '14 at 15:55
  • Cool, if you want to post an answer I'll delete this one as thats a better approach, and thanks for the tip - learnt something new today! – Phil Hawthorn Jan 16 '14 at 15:57
  • 1
    If I remember correctly, there is a problem with addFields not working in test context. As a result I favoured using the hidden field on the VF page. This describes it developer.salesforce.com/forums/… – Doug B Jan 16 '14 at 16:25
1

Easy fix: Since you are using a Standard Controller, you need to either include that field on the VF page somewhere (so the StandardController does the querying), or you need to query it yourself.

try this:

public newConstructor(ApexPages.StandardController standardController) {
    id billAccountId = standardController.getId();
    billAccount = [SELECT id,Bill__c
                   FROM Bill_Account__c
                   WHERE id=:billAccountId];

    Bill__c bill = [SELECT Id, Bill_Field1__c, Bill_Field2__c, Bill_Account__c.Bill__c 
                    FROM Bill__c 
                    WHERE Id = :billAccount.Bill__c];

}//END init(ApexPages.StandardController standardController)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.