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
Tom Barad 11Tom Barad 11 

Passing Parameters to a Custom Controller from VisualForce Page

Just starting out and I have a simple issue.  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.

Here is my 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: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 custom controller:
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;
    }
    
}
Eventhough my picklist field Marketo_Merge_Field_X__c has a value, when I look at my Log, I see:
2:47:01:062 USER_DEBUG [23]|DEBUG|mergefieldtext: null
Thanks in advance.

 
Sforce.NinjaSforce.Ninja
Hiya there Tom,
From what I think the problem is with your Campaign, ideally don't merge Standardcontroller and Custom in such a manner. If you declare your Campaign as Public Campaign you can directly refer it on the page and pass values. 

So your controller will become (Warning: Untested code ahead)
 
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: '+c.Marketo_Merge_Field_X__c );
       return null;
    }
    
}
And the page
<apex:page standardController="Campaign" extensions="MarketoMergeFieldExtension">
    <apex:form >
        <apex:pageBlock title="Marketo Merge Field Setter">
            <apex:PageBlockSection >
                <apex:outputField value="{!c.Name}"/>
            </apex:PageBlockSection>
            <apex:PageBlockSection title="Choose Marketo Merge Fields">
                <apex:inputField  value="{!c.Marketo_Merge_Field_X__c}"/>
            </apex:PageBlockSection>
            <apex:PageBlockSection >
                <apex:commandButton action="{!processMarketoMergeField}" value="Go!"/>
            </apex:PageBlockSection>            
        </apex:pageBlock>
    </apex:form>
</apex:page>

Temme if this solves your problem, if it does make this as an Answer so it helps others. 

 
Tom Barad 11Tom Barad 11
Thanks so much.  Totally makes sense.  I got an error with your code, but was able to resolve it. 

The corrected page code is:
<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>

 
Sforce.NinjaSforce.Ninja
Did that solve the problem? You need to declare the Campaign record in your controller. Let me know what was the error, I did not test the code, just modified it on the page. 
James LoghryJames Loghry
Your original code was correct per se, but there is (and has been for a long time) a known issue with Visualforce and the command button.  When you use the assign to attribute for a parameter, you have to specify the rerender attribute on your apex:commandButton tag.  For more info, see: https://help.salesforce.com/apex/HTViewSolution?id=000002664&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000002664&language=en_US)