9

I have an <apex:actionfunction> that updates a record based on an ID parameter I send it.

<apex:actionfunction action="{!UpdateRecord}" name="UpdateRecord" rerender="Errors" oncomplete="EnableInput();" status="SaveStatus">
    <apex:param id="UpdateRecordId" name="UpdateRecordId" value="" assignTo="{!UpdateRecordId}" />
</apex:actionfunction>

Since this ActionFunction is triggered by multiple elements on a page, I want to be able to determine which element caused the ActionFunction to run. Ideally, I'd like to change the CSS class of the element based on whether or not the ActionFunction processed successfully.

Is this possible?

8

Yes, you just need to expose the result of the action function to the visualforce page and make sure it gets rerendered as a result of the action function.

Here's an example that would make the result available in JS for your oncomplete function.

Visualforce

<apex:outputPanel id="resultVars">
  <script>
  var actionResult = "{!JSENCODE(actionResult)}";
  </script>
</apex:outputPanel>

<apex:actionFunction action="{!foo}" name="foo" rerender="resultVars" 
    oncomplete="completeHanlder();"/>

Controller

public String actionResult { get; private set; }

public void foo() {
  // do something
  if(success) {
    actionResult = 'Success!';
  } else {
    actionResult = 'Error';
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.