[SalesForce] Passing a Visualforce InputField to a Controller Extension

Sorry for the newbie question … I'm just starting out and I have a simple issue that I'm hoping someone will be able to help me with. I'm not able to pass a value from an inputField on a VF page to a custom controller. Based on my searching, it should be easy. Any help would be appreciated.

My Visualforce Page:

<apex:page standardController="Campaign" extensions="MarketoMergeFieldExtension">
    <apex:form >
        <apex:pageBlock title="Marketo Merge Field Setter">
            <apex:PageBlockSection >
                <apex:outputField value="{!campaign.Name}"/>
            </apex:PageBlockSection>
            <apex:PageBlockSection title="Choose Marketo Merge Fields">
                <apex:inputField  value="{!campaign.Marketo_Merge_Field_X__c}"/>
            </apex:PageBlockSection>
            <apex:PageBlockSection >
                <apex:commandButton action="{!processMarketoMergeField}" value="Go!">
                     <apex:param name="mergefieldtextparm" value="{!campaign.Marketo_Merge_Field_X__c}" assignTo="{!mergefieldtext}"/>
                </apex:commandButton>
            </apex:PageBlockSection>            
        </apex:pageBlock>
    </apex:form>
</apex:page>

And here is my extension:

public class MarketoMergeFieldExtension {

    public String mergefieldtext {get;set;}
    private final Campaign c;

    public MarketoMergeFieldExtension(ApexPages.StandardController controller) {
        this.c = (Campaign)controller.getRecord();
    }

    public PageReference processMarketoMergeField() {
        system.debug('mergefieldtext: '+mergefieldtext);
       return null;
    }   
}

Even though my picklist field Marketo_Merge_Field_X__c has a value, when I look at my Log, I see:

1   2:47:01:062 USER_DEBUG [23]|DEBUG|mergefieldtext: null

Thanks in advance.

Best Answer

Thanks for answering ... Got this to work (thanks, Sforce.Ninja on developer.salesforce.com!). Just posting the answer in case someone else comes across this.

The problem was that I set "Campaign c" to private and overthought the passing of parameters ...

Here is the fixed VF page:

<apex:page standardController="Campaign" extensions="MarketoMergeFieldExtension">
    <apex:form >
        <apex:pageBlock title="Marketo Merge Field Setter">
            <apex:PageBlockSection >
                <apex:outputField value="{!campaign.Name}"/>
            </apex:PageBlockSection>
            <apex:PageBlockSection title="Choose Marketo Merge Fields">
                <apex:inputField  value="{!campaign.Marketo_Merge_Field_X__c}"/>
            </apex:PageBlockSection>
            <apex:PageBlockSection >
                <apex:commandButton action="{!processMarketoMergeField}" value="Go!"/>
            </apex:PageBlockSection>            
        </apex:pageBlock>
    </apex:form>
</apex:page>

Here is the fixed extension:

public class MarketoMergeFieldExtension {

    public String mergefieldtext {get;set;}
    public Campaign c {get;set;}

    public MarketoMergeFieldExtension(ApexPages.StandardController controller) {
        this.c = (Campaign)controller.getRecord();
    }

    public PageReference processMarketoMergeField() {
        system.debug('mergefieldtext: '+mergefieldtext);
       return null;
    }   
}
Related Topic