How Do I: Use Script from a Web Service?

You can connect an ECMAScript function to your web service when you want to use the function for translating XML messages to Java or vice versa. When you have created the function in a JSX file, you connect the function to your web service by referencing it within an XML map.

Note: For step-by-step information on creating the script function itself, see How Do I: Create a Script for Use with a Web Service?

The procedures in this topic cover how to use script with parameters, or use script with a return value.

To Use Script with Parameters

  1. In Design View, double-click the arrow corresponding to the item (method, callback, or callback handler) with which the script function will be used. The Edit Maps and Interface dialog appears.

  2. In the top pane, locate the place in your XML map where you will reference your script function.

This will vary depending on how you are using script.

  1. In the top pane, enter a reference to your script function.

Your function reference should have a syntax similar to the following:

The items in parenthesis after the function name must correspond in name and order to the Java parameters that the script will use:

<placeOrder xmlns="http://www.openuri.org/">    <order_info>        {CustomerServices.OrderScripts.convertOrder(currentOrder, currentCustomer)}    </order_info>    <customer_id>{currentCustomer.custID}</customer_id> </placeOrder>

This example might correspond to the following Java declaration:

public int placeOrder(Order currentOrder, Customer currentCustomer) {...}

  1. Click OK to close the Edit Maps and Interface dialog.

 

To Use Script with a Return Value

  1. In Design View, double-click the arrow corresponding to the item (method, callback, or callback handler) with which the script function will be used. The Edit Maps and Interface dialog appears.

  2. In the top pane, locate the place in your XML map where you will reference your script function.

This will vary depending on how you are using script.

  1. In the top pane, enter a reference to your script function.

Your function reference should have a syntax similar to the following:

The item in parenthesis after the function name must be "return", as in the following example:

<placeOrderReponse xmlns="http://www.openuri.org/">    <order_confirmation>        {CustomerServices.OrderScripts.convertOrderResponse(return)}    </order_confirmation> </placeOrderResponse>

This example might correspond to the following Java declaration:

public int placeOrder(Order currentOrder, Customer currentCustomer) {...}

  1. Click OK to close the Edit Maps and Interface dialog.

Function References May Not Have Siblings

The function reference (everything between and including the curly braces) must be the only child of its parent element. For example, the following XML map fragment will cause an error:

<placeOrder xmlns="http://www.openuri.org/">
    {CustomerServices.OrderScripts.convertOrder(currentOrder, currentCustomer)}
    <customer_info>{currentCustomer}</customer_info>
</placeOrder>

But something like the following would work:

<placeOrder xmlns="http://www.openuri.org/">
    <order_info>
        {CustomerServices.OrderScripts.convertOrder(currentOrder, currentCustomer)}
    </order_info>
    <customer_id>{currentCustomer.custID}</customer_id>
</placeOrder>

Related Topics

How Do I: Create a Script for Use with a Web Service?

Using Script Functions From XML Maps