Creating New Entities through an OData Model

Objective

After completing this lesson, you will be able to create new entities for an OData service via an SAPUI5 OData model.

Batch Control

Group IDs

The OData V4 model sends requests in the following cases:

  • Implicit read requests to retrieve data for a binding

    For example, a list binding with the absolute path /UX_Customer triggers a GET UX_Customer to read data.

  • Implicit update requests via two-way binding

    For example, update a customer’s name through a property binding with the relative path CustomerName, which has a context with path /UX_Customer(00505604-4e85-1edd-818f-21e64b9cd2cf) triggering PATCH UX_Customer(00505604-4e85-1edd-818f-21e64b9cd2cf) with the customer name's value as JSON payload.

  • Explicit requests triggered through API calls like ODataListBinding.refresh() or ODataContextBinding.execute()

For each of these cases, it is possible to specify a Group ID of type string that allows you to group multiple operations into a single HTTP request payload (batch request).

OData batch requests use Group IDs: `$auto` for auto-batched, `$direct` for direct, and developer-defined Application Group IDs for custom batching.

A Group ID has one of the following submit modes to control the use of batch requests:

  • sap.ui.model.odata.v4.SubmitMode.API

    Requests associated with this submit mode are sent in a batch request via sap.ui.model.odata.v4.ODataModel#submitBatch().

  • sap.ui.model.odata.v4.SubmitMode.Auto

    Requests associated with this submit mode are sent in a batch request which is triggered automatically before rendering.

  • sap.ui.model.odata.v4.SubmitMode.Direct

    Requests associated with this submit mode are sent directly without batch.

The following Group IDs are possible:

  • $auto and $auto.*

    Predefined batch Group ID, which is the default if no Group ID is specified. You can use different $auto.*Group IDs to use different batch requests. The suffix can be any non-empty string consisting of alphanumeric characters from the basic Latin alphabet, including the underscore. They have the submit mode sap.ui.model.odata.v4.SubmitMode.Auto.

  • $direct

    Predefined batch Group ID, which has the submit mode sap.ui.model.odata.v4.SubmitMode.Direct.

  • Application Group IDs

    An Application Group ID is a non-empty string consisting of alphanumeric characters from the basic Latin alphabet, including the underscore. By default, an Application Group ID has the submit mode sap.ui.model.odata.v4.SubmitMode.API.

    On construction of the OData model, it is possible to define different submit modes for Application Group IDs. This is useful when you want to separate requests requiring short processing time on the server from those requiring long processing time, so that responses to "fast" requests are visible earlier on the UI.

Usage of Group IDs in XML Views

To specify a Group ID for implicit requests, use the following parameters for the binding: $$groupId – Group ID for read requests; $$updateGroupId – Group ID for update requests

In the example shown in the figure, Usage of Group IDs in XML Views, the binding context of the form is set to /UX_Customer(00505604-4e85-1edd-818f-21e64b9cd2cf) via the binding attribute of the <SimpleForm> tag. In addition, the Group ID for read and update requests is set to $direct ($$updateGroupId and $$groupId parameters). Therefore, the data for the form, that is, name and city of the customer with Id 00505604-4e85-1edd-818f-21e64b9cd2cf, is read directly without batch. Likewise, updates for the name and the city through two-way binding are sent directly without batch.

The default for the Group ID of the OData model is $auto. The value of Group ID is used as default for the Update Group ID of the OData model.

On instantiation of the OData model, you can also provide both a Group ID and an Update Group ID; if specified, these values are used as defaults if the corresponding binding parameters are not explicitly set.

For explicit requests, the Group ID can be specified as an optional parameter to the corresponding API method. The Group ID or Update Group ID of the binding is used as a default.

Creating an Entity

In the example shown in the figure, Creating an Entity - Example, a new customer is created when the Create Customer button on the UI is pressed.

sap.ui.model.odata.v4.ODataListBinding#create creates a new entity. For creating the new entity, the binding’s Update Group ID is used. sap.ui.model.odata.v4.Context#created returns a Promise that is resolved when the entity has been created in the back-end.

The sample application uses a table with Id customerTable on the UI, via whose items aggregation the customer entity set is displayed. For this entity set, a new entity is to be created.

The getBinding method is used in the event handler of the Create Customer button to query the binding object for the items aggregation of the UI table. This binding object is of type sap.ui.model.odata.v4.ODataListBinding. The create method of this list binding creates a new customer entity and inserts it at the beginning of the list. The method is passed the data for the entity to be created. The customer data is not shown in the coding example. For example, it can come from a form that the user has filled in.

For creating a new entity, the Update Group ID of the list binding is used. In the example, neither the $$groupId parameter nor the $$updateGroupId parameter is explicitly set for the list binding, so by default the $autoUpdate Group ID is used for the create operation. That is, the customer is created in the back-end via a corresponding batch request, which is automatically triggered before rendering.

The create method returns the context object for the newly created entity. It is of type sap.ui.model.odata.v4.Context. The created method of this context object returns a promise that is resolved without data when the entity represented by the context has been created in the back-end. The function registered for this case can be used, for example, to display a success message.