4

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++) {
     result2.push(records[i].TargetObjectId);
  }
  return result2;
}

document.write(result);

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

2
  • 2
    why not use javascript remoting instead of ajax toolkit as you are using a visualforce – Mohith Shrivastava Jul 29 '13 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 '13 at 17:04
2

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.

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.