0

I have a VF page to update couple of fields on the Opportunity record, it is a simple VF Page like below

<apex:page standardController="Opportunity" extensions="Opportunity_UpdExt" docType="html-5.0" sidebar="true" lightningstylesheets="true">
   <apex:form >
      <apex:pageBlock >
      <apex:pageMessages ></apex:pageMessages>
       <apex:pageBlockSection title="Interests" columns="1">     
        <apex:pageBlockSectionItem >
             <apex:outputLabel value="Select Type" for="typeSel"></apex:outputLabel>
             <apex:inputField value="{!oppRec.Type__c}" id="typeSel"/>
           </apex:pageBlockSectionItem>
           
            <apex:pageBlockSectionItem>
               <apex:outputLabel value="Interest"/>
               <apex:inputText value="{!oppRec.Interest__c}" />
            </apex:pageBlockSectionItem>
           <apex:commandButton value="Save" style="margin-left:500px;" action="{!finalUpdates}" />
         </apex:pageBlockSection>         
      </apex:pageBlock>
   </apex:form>
</apex:page>

And the Controller Extension is like

public class Opportunity_UpdExt{    
    public Opportunity oppRec{get;set;}
    public Opportunity currentRecord;        
    public Opportunity_UpdExt(ApexPages.StandardController controller) {
        Id currentRecId=((Opportunity)controller.getRecord()).Id;
        oppRec= new opportunity();
        currentRecord=[SELECT id,Type__c, Interest__c FROM Opportunity WHERE id=:currentRecId];
        oppRec = [SELECT id,Type__c, Interest__c FROM Opportunity WHERE id=:currentRecId];
    }

    public pagereference finalupdates(){
        currentRecord.Type__c= oppRec.Type__c;
        currentRecord.Interest__c= oppRec.Interest__c;        
        update currentRecord;     
        pagereference pageRef= new pagereference('/apex/Opportunity_Upd?id='+currentrecord.Id);
        pageRef.setRedirect(true);
        return pageRef;        
    }}

When I hit save the page is refreshed, but I was hoping two fields Type__c and Interest__c will be updated on the records with what was choosen on VF page. But when I query like these two fields are still blank even after executing VF Page, what am I missing here with respect to Updating the record

1

You don't need to query the records, just bind directly to the record in the controller:

<apex:page standardController="Opportunity" extensions="Opportunity_UpdExt" docType="html-5.0" sidebar="true" lightningstylesheets="true">
    <apex:form>
        <apex:pageBlock>
            <apex:pageMessages></apex:pageMessages>
            <apex:pageBlockSection title="Interests" columns="1">
                <apex:inputField value="{!Opportunity.Type__c}" id="typeSel" />
                <apex:inputField value="{!Opportunity.Interest__c}" />
                <apex:commandButton value="Save" style="margin-left:500px;" action="{!finalUpdates}" />
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

And your controller can be simply:

public class Opportunity_UpdExt{
    ApexPages.StandardController controller;
    public Opportunity_UpdExt(ApexPages.StandardController controller) {
        this.controller = controller;
    }

    public PageReference finalupdates(){
        if(controller.save() != null) {
            PageReference result = Pages.Opportunity_Upd;
            result.getParameters().put('id', controller.getRecord().Id);
            return result.setRedirect(true);
        }
        return null;
    }
}

Aside from that, I don't really see why it would fail, but having the overly complex setup is possibly hiding a bug somewhere.

4
  • Thanks for help. Sorry for amatuer coding. I was doing the query thinking it will be helpful later if we need conditional rendering of other field based on these field. Like if I want Interest_c to be conditionally rendered only when the value entered in Type__c is 'XYZ'. Is it possible with the above code by directly binding? – user81642 Jan 12 at 13:03
  • @user81642 Yes; the server will query all fields that are bound directly to the VF markup, and you add conditional rendering to fields using the rendered attribute. – sfdcfox Jan 12 at 17:16
  • Thanks, the issue is when I add the VF Page in to the Opportunity layout the Page refrence here takes me to to the to VF Page again when save is clicked instead of coming in to the Opportunity record back. – user81642 Jan 12 at 18:11
  • @user81642 That's what you told it to do? If you want to go back to the opportunity, return the value from standardController.save: public PageReference finalUpdates() { return controller.save(); } Or just use the standard action {!save} to save and go back to the opportunity. – sfdcfox Jan 12 at 18:30

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.