1

I want to use Extension on a Custom Object (MyCustom_Config__c). Then I want to access fields of custom object in Visualforce Page.

Visualforce Page:

<apex:page standardController="MyCustom_Config__c" extensions="ConfigControllerExt" showHeader="false" sidebar="false">
    <apex:form >
        <apex:pageMessages />

        <apex:pageBlockSection collapsible="false" columns="2"> 
                // Here we want to access fields of MyCustom_Config__c Object
        </apex:pageBlockSection> 
    </apex:form>
</apex:page>

Extension Class:

public with sharing class ConfigControllerExt {

    public final MyCustom_Config__c mpgcObj;
    ApexPages.StandardController stdController;

    public ConfigControllerExt(ApexPages.StandardController Controller) {
        stdController = Controller;
        this.mpgcObj = (MyCustom_Config__c)stdController.getRecord();
    }

}

My question is, If I want to use some field of MyCustom_Config__c, then how I will use it.

I know in case of Case object we can use

<apex:inputField value="{!case.Name}" />

Any help would be appreciated.

2

Your controller extension automatically has a property with the same name as the type specified in the Visualforce standardController attribute as if you had written code like this:

public with sharing class ConfigControllerExt {
    // Implicitly available property 
    public MyCustom_Config__c MyCustom_Config__c {get; private set;}
    ...

But that name can be quite clumsy to use repeatedly in Visualforce:

<apex:inputField value="{!MyCustom_Config__c.Name}"/>

so quite often you will see a more conveniently named property defined like this (that is an alias for the same object reference):

public with sharing class ConfigControllerExt {
    public MyCustom_Config__c mcc {get; private set;}
    public ConfigControllerExt(ApexPages.StandardController sc) {
        mcc = (MyCustom_Config__c) sc.getRecord();
    }
    ...

making the Visualforce references a little cleaner looking:

<apex:inputField value="{!mcc.Name}"/>
| improve this answer | |
  • When I am trying access those fields, it is throwing Visualforce Error : SObject row was retrieved via SOQL without querying the requested field: MyCustom_Config__c.Name I know why this error comes. But here I don't know, why it is coming? Is it expecting something before getRecords() method being called? – SFDC Geek May 23 '14 at 16:45
  • @SFDCGeek That is weird. Normally the Visualforce/StandardController combination work together with the set of fields to be queried by the StandardController being extracted from the expressions in the Visualforce page and automatically queried. There as a StandardController.addFields method to allow you to add other fields in but that isn't usually necessary. Do you get the error for both variants that I included in the answer? – Keith C May 23 '14 at 16:57
  • Yes I have used StandardController.addFields method. It worked. i think we are querying from Custom object, that may be the reason why it threw an Error – SFDC Geek May 23 '14 at 17:01
  • @SFDCGeek Well, again I don't think you should have to do that: I have custom object controller extensions and Visualforce that don't. If this is a once-off then no worries. But maybe the next time you want to do something similar it would be worth trying to figure out what is going on a bit more. – Keith C May 23 '14 at 17:06
1

You have defined the variable mpgcObj in your controller class as public, so it makes sense that you would use that variable in your page. Before that's possible, however, you need to give it getter and setter methods, which allow communication between the page and the controller. The simplest way to do that is to add { get; set; } at the end of the variable declaration:

public MyCustom_Config__c mpgcObj { get; set; }

Then in your VF page you can access this variable directly in your inputfield:

<apex:inputField value="{!mpgcObj.Name}" />
| improve this answer | |
0

You must add a call to addFields at the top of your extension constructor.

NOTE: there is a trick to this though -- you cannot use addFields within test methods, so you have to wrap it in a Test.isRunningTest(). To then get the same functionality from tests, you just need to pass in an object with the desired fields already populated to your StandardController.

public with sharing class ConfigControllerExt {

    public final MyCustom_Config__c mpgcObj;
    ApexPages.StandardController stdController;

    public ConfigControllerExt(ApexPages.StandardController Controller) {
        stdController = Controller;
        if (!Test.isRunningTest()) {
            stdController.addFields(new List<String> {'Field1__c', 'Field2__c'});
        }
        this.mpgcObj = (MyCustom_Config__c)stdController.getRecord();
    }
}
| improve this answer | |

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.