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
somasoma 

Using onSuccess and onFailure

There are a few functions which do not seem to return values themselves and, instead, mandate the use of onSuccess and onFailure, such as sforce.connection.remoteFunction(). I have only seen examples of how these methods are to be used, but documentation is otherwise sparse. I'd like to be able to call sforce.connection.remoteFunction() synchronously and if it is successful, continue executing other actions, but making a call from within an onSuccess function seems limiting. For instance, I have a function foo() that expects an array of data, but I only want to call this function if the remoteFunction() call was successful. If I generate the array of data before I call remoteFunction(), then I want to pass this array of data to foo() when remoteFunction() is successful, how can this be accomplished? Do I simply have to make this variable array global? Should I simply rework my logic so that I generate the array of data within foo()?

Ideally, I would like to be able to set some variable success in onSuccess and onFailure and then check its value outside of the scope of remoteFunction(), but even with global variables this doesn't seem to be working.
cheenathcheenath
You can make a sync call to remoteFunction by setting async = false:

   sforce.connection.remoteFunction({
        url : "http://www.cheenath.com",
        onSuccess : function(response) {
            log("Got response" + response);
        },
        onFailure : function(error) {
            log("ERROR: " + error);
        },
        async: false
    });




somasoma
Would this sync call cause remoteFunction() to return a value or would I still have to achieve what I wanted by going through onSuccess?
cheenathcheenath
You still have to go through onSuccess, remote function will not return the result.


somasoma
Thanks for your reply.

The primary question I had still remains. If I wish to base execution on the result of a remoteFunction() call and I must go through its onSuccess and onFailure methods, then is there a way to do one of the following:

1. Set a variable within these methods that can be accessed outside these methods, outside of remoteFunction(), e.g.:


var success;

sforce.connection.remoteFunction({
    url: "http://www.cheenath.com",
    ...
    onSuccess: function() { success = true; },
    onFailure: function() { success = false; }
});

2. Call a function from within these methods, passing to that function some prepared data.

I don't believe it's possible to accomplish either unless the variable I'd like to access was created globally. Is this correct?

Message Edited by soma on 10-16-2007 01:55 PM

cheenathcheenath
Try this:

function myFunction() {

    var success = false;
    var data;
 
    sforce.connection.remoteFunction({
        url : "http://www.cheenath.com",
        onSuccess : function(response) {
            success = true;
            data = response;
        },
        onFailure : function(error) {
            success = false;
            alert(error);
        },
        async: false
    });

    alert("got success " + success);
    alert("got data " + data);
}

myFunction();

Variables need not be global. Javascript closure [1] will keep the method variables in scope.

HTHs,
Manoj

[1] http://www.jibbering.com/faq/faq_notes/closures.html#clClose