function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mattdarnoldmattdarnold 

actionFunction - handle failure

Hi all,

I am building an application that makes use of the actionFunction component with parameters to take in data from the page and make inline updates to the database with no page refresh. I have this working, but I am trying to build some functionality to handle situations when the updates fail – the user is offline, the call times out etc. so I don’t miss updates.

I’ve built the code below that uses a Boolean true/false that gets updated when the function completes successfully then passes that to an inputText area on the screen to be read by my oncomplete JS function in the actionFunction (and then set back to false to handle the next updates from the user). This works, but is there an easier or more reliable way to handle this?

Open to any ideas. Thanks.

-- Matt

 

:: Page ::

 

<apex:page controller="reportActionTestController">

<script type="text/javascript">
// Javascript function to check value of outputForm.outputField and execute
// an alert and actionFunction fnCompleteReset only if the component
// outputForm.outputField is true
function fnCompleteAlert() {
if(document.getElementById('{!$Component.outputForm.outputField}').value=="true"){
alert("Function called, if is true, and fnSuccess=" +
document.getElementById('{!$Component.outputForm.outputField}').value);
window.fnCompleteReset(false);
} else {
alert("Function called, if is false, and fnSuccess=" +
document.getElementById('{!$Component.outputForm.outputField}').value);
}
}
</script>

<!-- inputText field to hold contents of fnSucess and is rerendered by fnComplete
actionFunction - this would be a hidden field, but is left displayed for
demonstration purposes -->
<apex:form id="outputForm">
<apex:inputText id="outputField" value="{!fnSuccess}" />
</apex:form>
<br/>
<br/>

<!-- Button to call fnComplete actionFunction -->
<apex:outputPanel onclick="window.fnComplete()" styleClass="btn">
Click Me
</apex:outputPanel>

<!-- Form contains actionFunction components to call fnComplete and fnCompleteReset
from the button and JS function actionStatus displays status for fnComplete -->
<apex:form id="theForm">
<apex:actionFunction name="fnComplete" action="{!fnComplete}" status="functionStatus"
oncomplete="fnCompleteAlert()" rerender="outputField" timeout="30000" />
<apex:actionFunction name="fnCompleteReset" action="{!fnCompleteReset}"
rerender="outputField" timeout="30000" />
<apex:actionStatus startText="Calling fnComplete..."
stopText="fnComplete call finished." id="functionStatus" />
</apex:form>
</apex:page>

 

:: Controller ::

 

public class reportActionTestController {

// Boolean variable to switch between true/false to determine
// function success or failure, defaulted to false when page loads
// and only set to true when main function completes successfully
public Boolean fnSuccess = false;

public Boolean getfnSuccess() {
return fnSuccess;
}

public void setfnSuccess(Boolean b) {
fnSuccess = b;
}

// Main function, this would be where data updates are processed
// and then fnSuccess is set to true, for this example, the function
// just sets fnSuccess to true
public void fnComplete() {
fnSuccess = true;
System.debug('fnComplete Called fnSuccess=' + fnSuccess);
}

// Function to reset fnSuccess after fnSuccess has been verified
// true by JS function
public void fnCompleteReset() {
fnSuccess = false;
System.debug('fnCompleteReset Called fnSuccess=' + fnSuccess);
}
}

 

 

 

sfdcfoxsfdcfox

I've never actually thought about that scenario before... but there really isn't a way to detect offline failures etc in your own code without doing something like what you've done. Visualforce assumes that failure is not an option, although there should be some logic in the library that will provide some sort of fallback mechanism (not that it helps your specific use case, but there you have it).