3

I try to pass a parameter from Visualforce to the controller via apex:param and then upsert the object in the controller. However, the parameter I passed on, "Field__c" did not get written to the database. Here is my code:

<apex:commandButton value="Save" action="{!Save}">
    <apex:param value="Saved" AssignTo="{!item.Field__c }" name="Saved"/>
</apex:commandButton>

Controller:

public PageReference Save() {
    try {
        item.NumberContacts__c = itemOld.NoContacts__c;
        update item;
    }   
    catch(Exception e){
        System.debug('Exception occurred '+String.valueOf(e));
        ApexPages.addMessages(e);
        return null;
    }
    return null;
} 

After reloading, item.Field__c should get displayed on an output field on the Visualforce page, but after hitting the command button, it still displays the old value, not the value "Saved". What do I miss here?

4
  • You'd have to query and rerender the updated information if you are not navigating to standard detail page.
    – Raul
    Commented May 3, 2017 at 19:32
  • Does your object item has the Field__c in its definition inside the controller? in other words, where is Field__c in the controller?
    – Mahmood
    Commented May 3, 2017 at 19:35
  • I retrieve the information for the object item via soql initially. Via apex:param the value for item.Field__c gets updated. I do not think I need to query again. The new value does not even get written into the database, so I do not think rerendering helps much as a first step. Commented May 3, 2017 at 19:36
  • Yes, Field__c gets queried for "item" in the getter method of the controller Commented May 3, 2017 at 19:38

2 Answers 2

2

Create a property in your controller, like

public propField {get;set;}

and assign it to the item.field__c like

item.field__c = propField;

Now, in your VF, set the property, like

AssignTo="{! propField }"

Explanation

When your VF page loads, it will feed the property propField with the parameter. Once your object item is ready, you can then set its Field__c value with the property 'propField' inside the controller.

As you might have already guessed now, that using item.Field__c didn't do anything because your object might not had been ready when the VF page loaded.

Update

As per the documentation, apex:param can only be a child of the following items:

  • <apex:actionFunction>
  • <apex:actionSupport>
  • <apex:commandLink>
  • <apex:outputLink>
  • <apex:outputText>
  • <flow:interview>

Try, changing your commandButton to commandLink.

What's wrong with CommandButton?

Apparently, there seems to be bug with setting the property with this component if apex:Param is used. Thanks to Jeff Douglas, there is a workaround to use CommandButton and still be able to set the property value.

<apex:commandButton value="Save" action="{! Save }" rerender="hiddenBlock">
    <apex:param name ="Saved"
                value="Saved"
                assignTo="{! propField }"/>
    </apex:commandButton>

<apex:pageBlock id="hiddenBlock" rendered="false"></apex:pageBlock>

All Hail, Jeff! for the CommandButton workaround.

2
  • Thank you, with propField the update functions well, just that the value "Saved" still does not get passed on and propField is null. Commented May 3, 2017 at 20:45
  • @user40501 CommandButton doesn't seem to be supported by the apex:param, try changing it to CommandLink.
    – Mahmood
    Commented May 3, 2017 at 20:57
0

Looks more like the rerender might have been your issue. For anyone else running into similar issue.

Per, apex:param not assigning value within apex:actionSupport for apex:inputCheckbox

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .