How can I make a service usable in different contexts (field names...)

So we have defined a simple service (SEARCH) to get a list of another type of records. Once the user has selected the value then we call another service (GET) to get the selected record and map some of the values to other fields in the form.

Now, we have more than a handful of "equal" fields that need to have the same lookup - and update "adjacent" fields to the one doing the lookup. How can we define the GET service to allow us to specify the fields to fill in after having retrieved the selected record?

I am trying to understand the use case and I think there are still some gaps.

User enters some search criteria and they get a list of records (in a table or dropdown?). Then the user selects one, which calls a service to retrieve the specific fields/values of that selected record. I presume they are mapped to a set of fields somewhere on the form. The user is then able to edit those values and then do you have a service that pushes the updates back to the original source?

Each service that you create is re-usable, but only for the context for which it is configured.

If you setup fields on your form that can be used to pass values to the service then you can enable the service to be more dynamic and respond to the user input which may yield different results based on what is provided.

I don't quite understand what you mean by "we have more than a handful of "equal" fields that need to have the same lookup - and update "adjacent" fields to the one doing the lookup"

If you can provide more detail I can try to provide a more specific response.

Ok, so to just keep it simple:

We have 8 "groups" of fields with e.g. a F_No1 and F_Name1 set of fields (i.e. next would be: F_No2 and F_Name2, ... and last would be: F_No8 and F_Name8).

In the F_Nox field we call a "Search" service (i.e. no input field mapped) to show a list of employees (with name and number). Once the user selects an employee and set the value of the F_Nox field (defined as Show name/number and save number), then in "onItemChanged" we call another "Get" service to the same set of data (i.e. there is an input field mapping to the F_Nox the user has just changed the value of). Once we have the record returned then we can set the F_Namex to the name returned from the service call.

Obviously, we can solve this by defining 8 x 2 services - but we would rather only have one service to return all of the values and one service to get the selected employee to be able to return the name (if we cannot get that from the returned data from the "Search" service).

OK, What I would do is defined a few hidden fields which will be used as the input and output of the service call. Then before calling the service you would copy the value from the visible field into the hidden one and call the service. Then when the service is complete you would copy the response from the hidden field to the appropriate visible field.

To accomplish this you would need a global variable to keep track of which set of fields triggered the service, something like: app.getSharedData().serviceTriggeredBy = xxx (I would store the ID or the number so you can use it later.

So it might look like:

app.getSharedData().serviceTriggeredBy = item.getId().substring(item.getId().length()-1); //this will just pull the number from the id off the end of the id

BO.F_ServiceInput.setValue(...);

form.getServiceConfiguration("SC_theService").callService();

Then to perform the post service actions you would use an onCallFinishedHandler placed in the form onLoad:

var srv = form.getServiceConfiguration('SC_theService');
srv.connectEvent("onCallFinished", function(success)
{
if(success) {
try {
//copy value from hidden response field to the appropriate visible field.

var fld = get(BO, "F_xxx" + app.getSharedData().serviceTriggeredBy);

if(typeof fld !== "undefined") {

fld.setValue(BO.F_response.getValue());

}
} catch(err) {
alert("SC_theService: " + err);
}

});

Yes, that is the approach we're implementing now. I just wondered if there was a more elegant or "clean" way to do it ;-)

I expect we can "clear" the values of the temp fields when saving the document - but not there yet...

Thanks!

I would clear the temp fields after each successful service call, in the onCallFinished handler.