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
fundooMentalfundooMental 

Updating a record through controller extension.

I have a cutom Object called "Experiment__c". On the record detail page I have Created a button which invokes the below visualforce page
And on this VF page I have created a button and written an extension class. On this extension class I am updating the current record (The record on which the detail page button was clicked to invoke this visualforce page) on the click of the command button.
But it does nothing. What am I doing wrong?

----VF PAGE-------------

<apex:page standardController="Experiment__c" tabstyle="Experiment__c" extensions="ExperimentStandardControllerExtension">
<apex:form >
<apex:pageBlock title="Experiment Edit" tabStyle="Experiment__c" >
<apex:pageBlockSection title="Information" columns="1" >
<apex:commandButton value="Update" action="{!updateRecord}"></apex:commandButton>

<apex:inputField value="{!Experiment__c.Lab_Country__c}"/>
<apex:inputField value="{!Experiment__c.Date_Entered__c}" required="false"/>

</apex:pageBlockSection>
</apex:pageBlock>

---------Controller Extension------------

public class ExperimentStandardControllerExtension {
Experiment__c ex = new Experiment__c();
Id id;

    public ExperimentStandardControllerExtension(ApexPages.StandardController controller) {
     ex = [SELECT Id,Lab_Country__c,Date_Entered__c FROM Experiment__c WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
     system.debug('Experiment LAB=' + ex.Lab_Country__c);                              
    }
   
    public pagereference updateRecord(){
    update ex;
    system.debug('Experiment LAB=' + ex.Lab_Country__c);
    return null;}

}
fundooMentalfundooMental
Ok I see. if I use the below code, then update works.

  public ExperimentStandardControllerExtension(ApexPages.StandardController controller) {
     this.ex = (Experiment__c)controller.getRecord();
     system.debug('Experiment LAB=' + ex.Lab_Country__c);                              
    }

But what is wrong when I get the record through the below code?  

public ExperimentStandardControllerExtension(ApexPages.StandardController controller) {
     ex = [SELECT Id,Lab_Country__c,Date_Entered__c FROM Experiment__c WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
     system.debug('Experiment LAB=' + ex.Lab_Country__c);                              
    }
Robert HowellRobert Howell
@fundooMental did you ever solve your problem? I am having a similar issue.