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:
Controller Code
Button
Note I changed the param Name to theId to avoid potential conflicts
Button
Controller
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.