0

I am trying to save value user select on picklist field to Opportunity object field. my visual force page is as follows.

<apex:page id="thePage" standardController="Opportunity" extensions="Extension" showHeader="false" sidebar="false" docType="html-5.0" >
    <apex:pageBlockSection columns="1" title="Rent" collapsible="false" id="Rent" rendered="{!NOT(readOnly)}" >
        <apex:pageBlockSectionItem >
            <apex:outputLabel value="Type" for="Type" />
            <apex:inputField value="{!Opportunity.Type__c}" id="Type" >
                <apex:actionSupport event="onchange" reRender="Table" />
            </apex:inputField>
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
            <apex:outputLabel value="Add" for="Calc" />
            <apex:inputField value="{!Opportunity.Calculate__c}" id="Calc" />
        </apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem >
            <apex:commandButton value="Add" action="{!Add}" />
        </apex:pageBlockSectionItem>
    </<apex:pageBlockSection>
</apex:page>

and extension is as follows

public class Extension {
    public ApexPages.StandardController stdCtrl;
    public Opportunity  la   {get;set;}
    private String  laId;

    /* Contstructor */
    public Extension(ApexPages.StandardController stdCont) 
    {
       this.laId = stdCont.getId();

       stdCtrl = stdCont;

    }

     public PageReference Add()
     {
        return stdCtrl.save();
     }
}

it does not give any error but not insert or update field value. Any help is really appreciated.

  • Sorry, but, I'm not sure I understand the question. What are you expecting to happen that's not happening? – sfdcfox Mar 18 '16 at 17:06
  • @sfdcfox As I am using extension with standardcontroller on my Visual force page, I have to override Save() . So from visual force page when user click on Add button I want to store thise two fields on Opportunity fields. Currently it is only displaying there and not storing user input. – Ricky Mar 18 '16 at 17:09
  • 1
    I do not see any code in your Add() method. If you are asking about trouble with that method it would be advisable to show the code you are actually having trouble with. Otherwise the answer is: Write the code in the Add method to update the record. It addition the page will not compile as there is no property named opp in the extension. – Eric Mar 18 '16 at 17:32
  • 1
    for reference for those of you trying to help: salesforce.stackexchange.com/questions/114796/… – Jesse Milburn Mar 18 '16 at 17:38
4

The record you receive in your constructor is where the changes made by the user will appear. In the other question, you query the record back from the database, thus ignoring the user's input. Ideally, your extension should look like this:

public class Extension {
    ApexPages.StandardController c;
    public Extension(ApexPages.StandardController ctrl) {
        c = ctrl;
    }
    public PageReference add() {
        return c.save();  // Saves user input
    }
}

If you need to manipulate data around before saving, you can do so by modifying the record contained in the StandardController:

public PageReference add() {
    Opportunity o = (Opportunity)c.getRecord();
    o.Name = someNewValue();
    return c.save();
}
| improve this answer | |
  • Thanks for your answer. I don't required to manipulate data. I only want to store user input. So I have tried your first solution. But that doesn't work. those fields on my Detail screen page is still showing no value. – Ricky Mar 18 '16 at 18:05
  • @Ricky I just noticed that you're binding to the variable you got from the standard controller. I expect this might be a race condition of sorts. Try changing {!opp to {!Opportunity and see if that fixes it. – sfdcfox Mar 18 '16 at 18:10
  • I am sorry I didn't get you . where do I have to replace {!opp with {!opportunity ? – Ricky Mar 18 '16 at 18:18
  • Ohh I got it, You meant on visual force page? Let me try that quickly,. – Ricky Mar 18 '16 at 18:21
  • I have changed that to {!Opportunity on my visaul force page that still doesn't store values. – Ricky Mar 18 '16 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.