Connector for OData – Offer data – Functions

The OData specification defines so-called function imports, with which functions or methods can be executed via an OData service. With the Intrexx OData provider, Groovy scripts can be executed on the server side via function imports, which are called via an OData request and which can receive input parameters and return values to the caller.

Here you can find out how to define functions and write the corresponding Groovy script.

To track the contents, a service should be created and enabled. You can find out how to create your own service here. The following description refers to the example used there. In principle, functions can of course be defined as required and, therefore, extend the standard of an OData service offering accordingly.

General properties

You can access the "Functions" dialog by double-clicking on the active OData service - here in our example with the name "Calendar1". This opens the service properties. The setting "Service is active" should be set here.

Data groups

In the example, the "Calendar1" application and the data group it contains are included under "Data groups". By clicking on "Edit data group", the set name ("IX_CAL_CALENDAR") can be determined and copied here, as this will be used later in our example function. Close the dialog with "Cancel".

Then select the "Functions" entry in the left-hand area.

Functions

Click on "Add function" to create a new function.

Function properties

Name

Give the function a unique name here which cannot contain special characters or spaces. For our example, we use the name "getIX_CAL_CALENDAR".

HTTP method

Then select the HTTP method which will be used to call the function. For our example, we use the "GET" method.

Entity set name (optional)

Defines the entity set on which the function operates. The set name that was previously determined in the data group properties is entered here.

Return type

Defines the type of return value of the function. This can be either an OData type (such as Edm.String, Edm.Int32, Edm.Double, etc.) or an Entity type. Information on the topic "Entity type as return type" can be found here.

In the example, the set name "IX_CAL_CALENDAR" is also used here.

Is collection

Defines whether the return value contains one or multiple data records/values.

Parameters

List of input parameters of a function. A new parameter can be added by clicking on "Add parameter" and an existing, highlighted parameter can be edited by clicking on "Edit parameter".

Edit parameter

A unique name and a data type must be defined for each parameter. The name must not contain spaces or special characters.

The type defines the parameter data type – either an OData data type (e.g. Edm.String, Edm.Int32, Edm.Double...) or an entity type.

The mode is purely informative and indicates whether the parameter is used for input or output or both.

Click "Finish".

Groovy script

The actual function logic is implemented with a Groovy script. The Groovy editor can be opened via the "Edit Groovy script" link at the bottom of the dialog. All standard Intrexx context objects and classes are available in Groovy. In addition to this, a number of OData-specific context objects are provided:

  • g_guidODataService (type String)

    Contains the GUID of the OData service.

  • g_oFunctionParameters (Map type)

    Contains the function parameters and their values.

  • g_oProducerContext (type IProducerCommandContext)

    Context object of the OData Producer. Provides access to the OData service configuration and the processing context.

  • g_oFunctionContext (type CallFunctionCommandContext)

    Context object of the OData function. Provides access to the function properties, the input parameters and values, and optional OData query string variables.

Access to function parameters in Groovy

Input parameters in a function can be accessed in Groovy as follows:

            def l_param = g_oFunctionParameters['BusinessPartnerName']

        

Definition of the return values of a function

The Groovy return value of a function must correspond to the data type defined in the function properties.

Example 1: Simple return value of type Edm.String

The following Groovy script returns the input value of type Edm.String as result:

            def l_param = g_oFunctionParameters['BusinessPartnerName']
def l_ret = "Parameter value: " + l_param
return l_ret

        

Example 2: Return value of type Entity Type

To define a single data record as the return value of the function, the fields of the Entity Type are added to a map, which are then returned.

            def l_param = g_oFunctionParameters['ID']
				// load record from database
				// return record value as entity type

				def l_entity = [:]
				l_entity['ID'] = 1;
				l_entity['DeliveryStatus'] = 'OPEN';
				l_entity['OrderStatus'] = 1;
				l_entity['BillingStatus'] = 'OPEN';

				return l_entity

        

Example 3: Return value of type entity collection

In this example, data records are loaded based on an input parameter, then the result returned as an Entity Collection (to clarify the example, the instructions to load the data records are not included):

            def l_param = g_oFunctionParameters['BusinessPartnerName']
				// load records from database
				// return record values as entities

				def l_entity1 = [:]

				l_entity1['ID'] = 1;
				l_entity1['DeliveryStatus'] = 'OPEN';
				l_entity1['OrderStatus'] = 1;
				l_entity1['BillingStatus'] = 'OPEN';
				l_entity1['TotalSum'] = 1200.00d;

				def l_entity2 = [:]

				l_entity2['ID'] = 2;
				l_entity2['DeliveryStatus'] = 'OPEN';
				l_entity2['OrderStatus'] = 1;
				l_entity2['BillingStatus'] = 'OPEN';
				l_entity2['TotalSum'] = 2300.00d;

				return [l_entity1, l_entity2]

        

A map is initialized for each data record here. Here, the field names are stored as a key and the values as values. Last, the map objects are stored in a Groovy list object and returned as the result. The OData Provider then converts the data structure corresponding to the data types automatically into the required OData format (JSON/XML).

Entity type as return type

Using the entity type as return type, the data sets of data groups can be supplied. Here is a sample configuration.

Click on "Edit Groovy script" to open the Groovy script editor.

Insert the following script there:

            return [["STRID":1,"STR_TITLE":"Hello world"],["STRID":2,"STR_TITLE":"Hello world2"]]
        

"STRID" corresponds to the data field name of the primary key, "STR_TITLE" is the data field name of the term title. Both data field names can be easily determined in the editor on the "Application structure" tab and inserted in the script.

Browser request

The data record collection can now be called up. The URL can be determined on the "General" tab. It corresponds to the "endpoint URL" there, to which only the part "<function name>?$format=json" is then appended.

If only a single data set is to be retrieved, formulate the Groovy script as follows:

            return ["LID":1,"STRHEADLINE":"Hello world"]
        

Also deactivate the "Is collection" setting on the "Functions" tab.

More information

General

System requirements

Consume data

Provide data

Integration in applications

Use in processes

Expert settings

Appendix