I am creating this Visual force page that will heavily rely on the Ajax tool kit and trying to do it asynchronous style, because I think that is what the cool kids are doing.

Is it not possible to return a value from the "success" function to a global variable "result" ? right now it is return undefined.

Here is a snippet of what I am trying to do:

var result = sforce.connection.query("SELECT TargetObjectId FROM ProcessInstance Where Id IN (SELECT ProcessInstanceId FROM ProcessInstanceWorkitem)", {
  onSuccess : success,
  onFailure : failure

function success(result) {
  var records = result.getArray("records");
  var result2 = new Array();

  var recLength = records.length
  for (var i=0; i<recLength; i++) {
  return result2;


Any thoughts, should I just do it the synchronous way?

  • 2
    why not use javascript remoting instead of ajax toolkit as you are using a visualforce Jul 29, 2013 at 16:20
  • Because that involves writing apex code, and what I am doing right now would be more of prototyping. Also, there is a small chance that this won't be within a VF page.
    – MCHam
    Jul 29, 2013 at 17:04

1 Answer 1


To do it asynchronously, simply assign result2 to result. The result (no pun intended) is that the reference to result will resolve to window.result, the global variable. It should be noted that document.write(result) wouldn't work in any case, unless the callback beats the parser (which it won't). You won't be able to use your exact design, as you'd have to use something like innerHTML or another construct to get the results directly into the page. This is related to the fact that you'd have to call document.open first, which would clobber the page.

You must log in to answer this question.

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