How to call an activity from the client using the "asyncRequest" JavaScript function

Here is a quick example where we execute the @baseclass.pyUtilityStub activity via an HTTP request using the POST method. In addition, we pass five parameters to that activity, three of them are passed in the query URL and the other two are passed in the body of the POST request. Note that we can pass the pzPrimaryPageName parameter to set its primary page.

var oSafeURL = new SafeURL("@baseclass.pyUtilityStub");
oSafeURL.put("pzPrimaryPageName", "pyWorkPage");
oSafeURL.put("parameterGet1", "valueGet1");
oSafeURL.put("parameterGet2", "valueGet2");

var postData = new SafeURL();
postData.put("parameterPost1", "valuePost1");
postData.put("parameterPost2", "valuePost2");

pega.u.d.asyncRequest("POST", oSafeURL, null, postData);

The parameters will be sent to the activity and we can verify it using the Tracer.

The pega.u.d.asyncRequest function is defined in the webwb/pzpega_ui_doc_ajaxengine.js text file rule.

If we don't pass the pzPrimaryPageName parameter, we may get a Java Exception like the following

Java Exception: 	com.pega.pegarules.pub.generator.RuleNotFoundException: Failed to find a 'RULE-OBJ-ACTIVITY' with the name 'AX_ARDEMO' that applies to '@baseclass'. There were 1 rules with this name in the rulebase, but none matched this request. The 1 rules named 'AX_ARDEMO' defined in the rulebase are:
1 related to applies-to class '@baseclass', but were defined on subclasses: 'Work-'.

Catching the response

Using the third parameter, we can catch the response of the activity through the success, failure, and timeout callbacks.

pega.u.d.asyncRequest("POST", oSafeURL, {
  success: function(o) {
    console.log(o);
    alert("SUCCESS!");
  },
  failure: function(o) {
    console.log(o);
    alert("FAIL!");
  },
  timeout: function(o) {
    console.log(o);
    alert("TIMEOUT!");
  }
}, postData)

I didn't test the failure nor the timeout callbacks.

Test it!

Download this component, add it to your application, and execute the Flow_ARDemo to see the asyncRequest in action.

References

Comments