Synchronizing Service Calls

You can retrieve data from any other application or from an external web service.  By default the services that are triggered off a single action will run asynchronously (at the same time).  Sometimes you want to control the order that services are called because they may be dependent on each other.  Or you might want to change something in your form as the result of performing a service call.

For example, let's say you have two services (serviceA and serviceB), we want serviceA to be triggered when we click a button, but then we want serviceB to ONLY be executed once serviceA has completed.  This article assumes that you have already created the services using the "Add Service Configuration" button on the Settings tab.

In order to do this we have to create a listener that will watch for the service to complete and then perform some work.  In my experience I would recommend creating the listeners in the onStart for your application so that they are all in one place.

Here is an example of creating a listener:


var form = app.getForm('F_Form1');                                                  //1

var srvA = form.getServiceConfiguration('SC_serviceA');              //2
srvA.connectEvent("onCallFinished", function(success)              //3
{
  if(success) {                                                                                         //4
     form.getServiceConfiguration('SC_serviceB').callService();    //5
  }
});


Let's break down each piece:

1. The first thing that we do is get a handle to the form object. In your case you would replace "F_Form1" with the ID of the form where you configured the service.

2. This line is creating a handle to the service called "SC_serviceA".  You can see this service on Settings...Services section.

3. We are connecting a function to the "onCallFinished" event of the service.

4. Inside of the function we verify that the call was successful.

5. Connect to "SC_serviceB" and execute it.  This is what triggers the second service.

It is really important to only create a single listener for an event for any given object.  There is no limit to the depth to which you can chain services, but keep in mind that it is only necessary to use this technique if the inputs for serviceB are returned from calling serviceA.

Related pages