0

I have a custom object standardcontroller and an extension. I have embedded a VF page on the custom object's pagelayout. When the new custom object is created, I want to be able to access the custom object fields in my extension.

<apex:page standardController="Custom_Object__c" extensions="MY_Controller">

My constructor looks like this (not sure if this matters):

public ApexPages.StandardController stdCntrlr {get; set;}
public My_Controller(ApexPages.StandardController controller) {
    stdCntrlr = controller;
}

So when I create Custom_Object__c I want to be able to access all Custom_Object__c fields in MY_Controller. (not just in the VF page) Any help greatly appreciated.

1

I would suggest you also go through a nice blog written by Bob, where he has explained everything about how to get extra fields of record in extension.

http://bobbuzzard.blogspot.in/2011/04/dynamic-visualforce-bindings-and.html

public class AddFieldsController 
 {
  public AddFieldsController(ApexPages.StandardController stdCtrl)
   {
      stdCtrl.addFields(new List<String>{'Name', 'BillingCity'});
       Account acc=(Account) stdCtrl.getRecord();
   }
 }

The above code will help developer to get Name and BillingCity also queried for him.

Hope this gives you an idea about what you are actually looking for. :)

  • stdCtrl.addFields(new List<String>{'Name', 'BillingCity'}); ---This is important or it complains about not having included the field in the SOQL – SkipVV May 25 '17 at 15:12
2

I highly recommend you complete this Trailhead module: Create a Controller Extension. It would also be worth reading up on Introduction to Controllers and Extensions.

You can get the record from the controller using the getRecord() method:

Custom_Object__c record = (Custom_Object__c)controller.getRecord();

Notice how I put the type in parentheses before instantiation. That is called casting (or type-casting). Since getRecord() returns an SObject instance, we have to tell the compiler that this particular instance is actually a more specific type, namely Custom_Object__c.

From the Visualforce context, all the field which are referenced on the page get pulled in. If you want to manipulate fields which are not needed on the page, there are multiple ways to pull in additional fields.

From a unit test context, the record will have all fields populated which you already specified. So if you create a test Custom_Object__c record and populate its Name, your controller would be able to use that even if the page doesn't reference the field.

  • Thanks! I will take the time and read through the documentation you provided. – SkipVV May 25 '17 at 16:34

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.