[SalesForce] apex:actionSupport with param is not passing updated value to controller

I have a VisualForce page that should pass the selected the value to the controller to update the record. See code below.

<apex:inputField value="{!l2.Status__c}">
<apex:actionSupport event="onchange" reRender="newstatus" action="{!saveChanges}" status="myStatus">
<apex:param name="statusId" value="{!l2.id}" assignTo="{!cid}"/>
<apex:param name="status" value="{!l2.Status__c}" assignTo="{!l1}"/>
</apex:actionSupport>
</apex:inputField>

When the value in inputField for {!l2.Status__c} is updated, it should pass the new value to the controller and assign to {!l1} in the controller. However, it doesn't work. If I use something such as <apex:param name="status" value="Test" assignTo="{!l1}"/>, it does pass the "Test" value to the controller and the record is update with the the value of "Test". But I cannot seem to get the user's selected value in {!l2.Status__c} to update in the record.

Best Answer

When you pass the values like that in the param you are passing the original value as the new value has not been set yet.

You should be:

<apex:inputField value="{!l2.Status__c}">
<apex:actionSupport event="onchange" reRender="newstatus" action="{!saveChanges}" status="myStatus">
<apex:param name="statusId" value="{!l2.id}" assignTo="{!cid}"/>
</apex:actionSupport>
</apex:inputField>

Then in your controller saveChanges method

l1 = l2.Status__c;

As when this is assigned the setter for l1 has fired and has the new value from the input field

This then poses the question of the need for the param at all for cID as you can just use l2.id in the controller as well. Taking it further, why use the l1 or cid at all then when you can just use the values l2.Status__c and l2.id

Related Topic