views:

435

answers:

1

I'm having strange results with a Visualforce page (yes, Salesforce.com is icky, I know). My issue is that I am trying to use the inputField to bind data on a custom sObject, but in my custom controller, it is not recognizing the user input data.

Here is the code snippet from the page:

<apex:pageBlockSection title="Enter New Fee" rendered="{!isRenderedFees}" >

    <apex:inputField value="{!workingFee.Fee_Type__c}"  required="True"/>
   <apex:inputField value="{!workingFee.Fee__c}"  required="True"/> 

    <apex:pageBlockSectionItem >
        <apex:CommandButton value="Save Fee" action="{!saveFee}"  immediate="true" />
        <apex:CommandButton value="Cancel" action="{!cancelFee}" immediate="true" />
    </apex:pageBlockSectionItem> 
 </apex:pageBlockSection>

and here is the code from the controller:

public Fee__c workingFee {get; set;}
 ....
public PageReference saveFee(){

    this.workingFee.Trade_Group__c = tradeGroup.id;
    try{
     System.debug('~~~~#~~#~~workingFee: '+workingFee.Fee_Type__c +'='+workingFee.Fee__c);
        upsert workingFee;
    }catch (System.Dmlexception e){
     ApexPages.addMessages(e);
        return null;
    }
    System.debug('~~~~#~~#~~workingFee: '+workingFee.Fee_Type__c +'='+workingFee.Fee__c);
    //savedFees.add(workingFee.clone());

    //re-render the page
    this.isRenderedFees = False;
    return null;
}

I've made sure the workingFee property is not null. Whenever I hit the "Save Fee" button after entering the values, it reloads the page and gives me the message "Error: Required fields are missing: [Fee__c]" (note, Fee__c here is a currency field -- it's not that it expects this to be an sObject, is it?)

The debug statement in the saveFee() method shows that workingFee's important fields are null, when I would expect them to have been assigned the values input by the user.

+2  A: 

I have had a whole heap of issues binding controls to a property exposed with the simple { get; set; } notation... The rest of your code will see the properties, but for some bizarre reason, your View won't (always) bind...

Try writing explicit get/set methods, like

private workingFee;
public Fee__c getWorkingFee() {
    return workingFee;
}

public void setWorkingFee(Fee__c value) {
    workingFee = value;
}

There is no logical reason why this should work any different to

public Fee__c workingFee { get; set; }

but in my experience, it sometimes does...

what did you say about it being icky? ;)

IanR
Thanks Ian, sadly, this does not seem to be the issue. I get exactly the same behavior with explicit setters and getters. I also will say that using properties works for OTHER sObjects in the same page/controller!
Ben
Drat - I'm glad I said 'sometimes'... it's very hit and miss.OK - so next guess... when are you instantiating the Fee__c object? I n the constructor? chuck a debug statement in your new get/set methods to see if the Fee__c object is null or not...
IanR
another good thought, but no, it's not null. I do instantiate it in the constructor. Debug logs indicate that the getter is being called, and the Fee object is not null; however, nowhere do I see the setter being called! What am I missing here?
Ben
so my next thought is that, since you are not validating the page (you have set immediate=true on the commandButton), maybe the saveFee code is being invoked before the setter is called? Try setting the immediate flag to false (i'm just about out of ideas, I'm afraid)
IanR
For the win! You are a champ. One wonders what the author of the documentation was thinking, leaving out that little tidbit.
Ben

related questions