[SalesForce] Pass parameters in a function through command button

How to pass parameters in a function through command button?

Method

public PageReference ContactsAdd(String Id){
    campaigns  = [Select id,Name from campaign where Id ='7017F000000TzY1'];
    CampaignMember cm = new CampaignMember(CampaignId='7017F000000TzY1', ContactId = + Id , Status = 'Sent');
    members.add(cm);
}

Button

<apex:commandButton value="Add to Campaign!" action="{!ContactsAdd}"> 
       <apex:param name="Id" value='0037F000004yM2m' assignTo="{!Id}"/> 
</apex:commandButton>

this gives me error :

unknown method'AccountStandardController.ContactsAdd()'

Best Answer

You cannot pass the value to a parameterized method like you are attempting to.

Using a remote action is one way to do it but it really does not add any value for this simple use case.

Remote action would be great if you have to pull values from inputs prior to invoking the method (when they are not set in the controller yet). When using the param the value has to exist / be concrete at the time the button is clicked.

Here are a couple ways to achieve the same goal using the param...

Here are two ways you could do it:

First way using a property - The assignTo on the param will assign the value to the Property

Controller Code

public String theId {get;set;} //This is one way - create property to assign to

public PageReference ContactsAdd(String Id){
    campaigns  = [Select id,Name from campaign where Id ='7017F000000TzY1'];
    CampaignMember cm = new CampaignMember(CampaignId='7017F000000TzY1', ContactId = theId , Status = 'Sent'); //use your theId here
    members.add(cm);
}

Button

<apex:commandButton value="Add to Campaign!" action="{!ContactsAdd}"> 
       <apex:param name="theId" value='0037F000004yM2m' assignTo="{!theId}"/> 
</apex:commandButton>

Note I changed the param Name to theId to avoid potential conflicts

Second way - Grabbing the param from the page - Using the name of the param to get the value

public String theId {  
    get{
        return Apexpages.currentPage().getParameters().get('theId');
    }
}

public PageReference ContactsAdd(String Id){
    campaigns  = [Select id,Name from campaign where Id ='7017F000000TzY1'];
    CampaignMember cm = new CampaignMember(CampaignId='7017F000000TzY1', ContactId = theId , Status = 'Sent'); //use your theId here
    members.add(cm);
}

Button

<apex:commandButton value="Add to Campaign!" action="{!ContactsAdd}"> 
       <apex:param name="theId" value='0037F000004yM2m'/> 
</apex:commandButton>

Full working example of the second way including how to use merge fields so you do not have to hardcode the value

Controller

public class myExample{

    public Account acc {get; set;}

    public myExample(){
        acc = New Account(Name = 'abcdef');
    }

    public void doStuff(){
        //Do stuff
    }

    public String theString {  
        get{
            return Apexpages.currentPage().getParameters().get('theString');
        }
    }   
}

Page

<apex:page id="myExamplePage" controller="myExample">
    <apex:pageMessages id="msgs"/>

    <apex:form >
        <apex:commandButton action="{!doStuff}" value="Click Me" rerender="thePanel">
            <apex:param value="{!acc.Name}" name="theString"/>
        </apex:commandButton>


        <apex:outputpanel layout="block" id="thePanel">

            {!theString}

        </apex:outputpanel>


    </apex:form>


</apex:page>

Simply load the page, click the button and watch the value of Account Name appear

NOTE The documentation does not say that the <apex:param> can be a direct child of a command button. It works but it is not documented so use at your own peril.