+ Start a Discussion
Derek Davis 7Derek Davis 7 

Guidance on creating Extension Controller?

Hi All! I have been an admin for a couple years and I am starting to get into some lightweight development. I have a Visualforce page and need it to automatically update the value of a custom picklist field. Could anyone offer any guidance on creating an Extension Controller? (Not sure it if matters, but this page is accessed by a "Complete Event" button. I was using URL Hacking to update field from button, but couldn't get this method to work when we switched to Visualforce page)

Below is the first line of my Visualforce page. It's my understanding that I would need to reference the extension here?

<apex:page standardcontroller="DGCampaign__c">

I need to change the Status of the record to "Completed".
Here is the line of code from my Visualforce page that references the Status field I need to update:

<apex:inputfield value="{!DGCampaign__c.Status__c}" required="false"/>

It is my understanding that I need to create an Extension (Apex Class) that contains Apex tags to update this field?

Could someone confirm if my very basic understanding is correct? And any guidance on what that class should look like would be extremely appreciated!! : )


Yes, URL hacking doesn't work for Visualforce page so you will need an extension controller (there are other ways to achieve this using JavaScript, but its probably best to go the extension controller route first).  Should be pretty simple:


public class CampaignExtCtrl
   // constructor - needs to take the standard controller as a parameter to be an extension controller (extends standard)
   public CampaignExtCtrl(ApexPages.StandardController std)
      // get the record from the standard controller
      DGCampaign__c camp=(DGCampaign__c) std.getRecord();
      // set the value of the status field


<apex:page standardcontroller="DGCampaign__c" extensions="CampaignExtCtrl">