Showing posts with label odata tutorial. Show all posts
Association and Navigation in OData

Association and Navigation in OData

Sanjo Thomas•23:47:00
In this blog we will be covering the concept of association and navigation in OData. 

In this tutorial, we will take example of navigating from one Sales Order to the related Sales Order line items, by using a link instead of manually putting a filter together. Moreover, it also allows us to use the $expand statement to fetch the Sales Order together will all the Sales Order line items in one call.

Lets say, we already have created an OData service in SEGW named ZGW100_XX_SO. Double click on Associations.
Press Create button.

Now, Enter the following values for association and press enter:
   

Name

SalesOrderSalesOrderItems

Principal Entity*

SalesOrder

Principal Entity Cardinality*

1

Dependent Entity*

SalesOrderItem

Dependent Entity Cardinality*

M


Now, create a referential constraint for the association
1) Expand the Associations node and the SalesOrderSalesOrderItems node and double-click Referential Constraints:















2) Choose the Create pushbutton:






3) Enter the following values and choose Enter:

Principal Key*

SoId

Dependent Property*

SoId

* This field has an Input Help




Now we create an association set for the association

1. Double-click Association Sets:


2. Choose the Create pushbutton:


3. Enter the values and choose Enter:

Name

SalesOrderSalesOrderItems

Association*

SalesOrderSalesOrderItems


And finally we create a navigation property based on the referential constraint

1. Expand Data Model > Entity Types > SalesOrder and double-click Navigation Properties:


2. Choose the Create pushbutton:

3. Enter the following values and choose Enter:

Name

SalesOrderItems

Relationship Name*

SalesOrderSalesOrderItems

Now we need to re-generate the runtime objects and we’re then ready to test the service

1. Choose the Generate pushbutton:

2. Verify that the runtime objects have been generated successfully:

3. Start the Gateway Client (Transaction /IWFND/GW_CLIENT) in a separate window to run the service. Provide the following URI to get the metadata for the service: 

/sap/opu/odata/sap/ZGW100_XX_SO_SRV/$metadata


The Sales Order collection now includes a navigation property.

4. When you now select a sales order entry using

/sap/opu/odata/sap/ZGW100_XX_SO_SRV/SalesOrderCollection(‘0500000001’), for example, you can simply add the navigation link /SalesOrderItems to navigate to the line items without having to set a filter yourself:


5. And finally you can use $expand to read all sales order items for a given sales order in a single http call.

Simply provide the following URI:

/sap/opu/odata/sap/ZGW100_XX_SO_SRV/SalesOrderCollection(‘0500000001’)/?$expand=SalesOrderItems


The $expand statement is handled by the framework (no additional implementation is required). Since the framework does not know that both entities can be obtained using a single RFC call, it executes two calls to the underlying BAPI. This can be improved by manually implementing (re-defining) the GET_EXPANDED_ENTITY method.

So we are done. The Service is up and running.


























Read more
Register OData Service

Register OData Service

Sanjo Thomas•22:13:00

In this blog, I will try to cover how to register OData service, once we have created an OData service in SEGW. After registering only we can access the SAP OData service. By registering, we mean we open our service to the internet. We can Register the service in two ways:

  • From the transaction SEGW
  • From the transaction /n/IWFND/MAINT_SERVICE

Steps to Register an OData service using SEGW

Once the OData is generated, now it is the time to register the service. Once the service is registered, then only it can be used anywhere.

To generate an OData, follow the given steps:

Double click the node Service Maintenance

Select any System and click Register as shown below:



Enter an Alias, if asked as shown below:



Now, in the next screen you get following options:

Field

Description

Technical Service Name

This is auto generated service name

Service Version

It is auto generated service version

Description

It is the Description of the OData you have mention earlier

External Service Name

It is same to the auto generated service name

Namespace

It is by default bank

External Mapping ID

It is by default Empty

External Data Source Type

It is by Default set to source type C

Technical Model Name

It is auto generated Model Name

Model Version

It is auto generated Model Version

Package Assignment

Assign your package name here, or just choose Local Object from the button below. It will be then $TMP

ICF Node

It is by default “Standard Mode”

oAuth Enablement

It provides option to add extra authentication, in case you need oAuth2 enablement choose it, else leave it blank

 

In our case, we just need to choose package and click confirm, as shown below:



Once the service is registered, the Registration will turn green, as shown below:



Test the OData service

To test the OData service, click the SAP Gateway Client button, as shown below:



It will open a new screen, here click Add URI Option as shown below:



Choose $metadata, here:



Click Execute as shown below:



If the status is 200, as shown above, then it means your OData is working fine.

Steps to Register an OData service using /IWFND/MAINT_SERVICE

Open the Service Maintenance Screen using the transaction /IWFND/MAINT_SERVICE. The given screen will open:



Click “Add Service” button. It will open “Add Selective Services” popup.



Filter your service by entering it in the field “External Service Name” as shown below and click the search icon.



Now, the above search will return your OData Service name. Select your service and click Add Selected Service



Now, assign your package name to the Service as shown below. In case, you don’t have package name, either click on “Local Object” or write $TMP in the package assignment. Then, save the above settings.



Now, a pop up will open saying “Service <your service name> was created and its metadata was loaded successfully

Now, go back to the transaction /iwfnd/maint_service and filter your service again. This time select your service and select SAP Gateway client



The Filter will open this popup, enter your details here and click execute (the green button).



Then, follow these steps:



Here, test your service by following the steps given below:

To test the OData service, click the SAP Gateway Client button, as shown below:



It will open a new screen, here click Add URI Option as shown below:



Choose $metadata, here:



Click Execute as shown below:



If the status is 200, as shown above, then it means your OData is working fine. In the next blog, I will cover how to activate the OData services in SICF.


Read more
How to perfrom CRUD operations in OData

How to perfrom CRUD operations in OData

Sanjo Thomas•00:14:00


In previous blog, we discussed Introduction to OData and how to create an OData service in SAP NetWeaver Gateway. We have also shown how to test an OData if it is working fine or not. Now, we need to learn how to perform operations like Insert, Update and Delete. In this article we will also learn basic queries of reading data.

How it works

In case of OData, there are three phases of data transfer. These are:

1. Sending Data from Frontend

Data is sent from UI (Client) to OData (Server) in form of query

2. Taking Data from Frontend

At OData we receive data from Frontend using data provider (Code implemented in CRUD Operation section)

io_data_provider->read_entry_data(IMPORTING es_data= ls_entity).

3. Sending Data to Frontend

From OData we send data to UI using et_entityset or er_entity after data manipulation (Code implemented in CRUD Operation section).

Operations in ODATA

In this section, we will explore all the methods generated automatically, once you generate an OData. In last article, we have already instructed to write all your CRUD operations related code in DPC_EXT class. Hence, we will follow the same concept here.

SAP ABAP OData provides different methods for CRUD operations, these are:

Method

SQL Operation

Description

GET_ENTITY

Select

This method is used to read a single data based on table keys

GET_ENTITYSET

Select

This method is used to read entire data of a table

CREATE_ENTITY

Insert

This method is used to create/insert a new data in table

UPDATE_ENTITY

Update/Modify

This method is used to update an existing data in table

DELETE_ENTITY

Delete

This method is used to delete an existing data in table

 

Query in SAP OData

In this section, we will explore all the Query we can perform while reading data from OData. In above section, we have learnt how to do read calls using GET_ENTITY and GET_ENTITYSET. Sometimes, we need to filter out data according to our requirement, or to get total number of data counts or to get data in specific order. These all SAP OData Queries are explained below:
Here, let us suppose <your service name>  = https://isd.sap.com/<OData Service Name>

Query

Description

Example

$metadata

It gives the metadata detail of your service. By metadata we mean it will provide information about all the entity sets with their field names and their attributes

<your service name>/$metadata

$FILTER

It is mainly used during Read Entity Set call. During Read, we can send some filter value in backend that can be later used in where condition of Select queries

<your service name>/$FILTER

$top and $skip

It is mainly used to get limited data in the UI, in case you are reading all the data, and the table is having a very large data

<your service name>/$top and $skip

$orderby

This is used to order the data in ascending or descending order for a specific key

<your service name>/$orderby

$format=json

This returns the data in form of JSON format. By default, the result is in XML format.

<your service name>/$format=json

$inlinecount

This returns the number of data of a table that will appear in the UI

<your service name>/$inlinecount

$expand

This is used to bind the association and navigation data together

<your service name>/$expand

$value

This is used to return the media data

<your service name>/$value


In the following steps, I will list down steps to create an entry through OData service. Similar steps are required for other CRUD operations.

To configure a well-defined SAP Gateway OData service, complete the following steps in your SAP system. These steps cover the general procedure, but need some consideration for your own SAP OData system and your preferred techniques for configuring SAP.

Display the SAP Gateway Service Builder (transaction SEGW)

Create a project

Figure 1. Select Create Project


Configure details for the project

Chose a project name, give your project a description, select Standard generation strategy, and specify the package. The project will be saved as a local object (Package: $TMP, or click Local Object.)

Figure 2. Create Project, specify details


Create the Entity type by importing a pre-defined DDIC Structure for the Order header.

Right-click Data Model, and then select Import > DDIC Structure.

Figure 3. Data Model, Import > DDIC Structure


Choose the required structure.

If the Sales order structure is not available in your SAP system, select the ABAP structure ZST_VBAK. Click Next.

Figure 4. Import from DDIC Structure, step 1 of 3


Choose the required fields, and then click Next.

Figure 5. Import from DDIC Structure, step 2 of 3


Select the key field, VBELN, and then click Finish.

Figure 6. Import from DDIC Structure, step 3 of 3 Result: You should now see the new Entity that has been created.


Repeat the same sequence of steps from 4 to 7 to import the next structure, OrderStatus.

This may be an Item structure. The following example shows the import of another custom structure called ZST_SO_STATUS.

Second Import from DDIC Structure, step 1 of 3

Second Import from DDIC Structure, step 2 of 3

Second Import from DDIC Structure, step 3 of 3


When you have completed the preceding steps, you should see the two newly-created Entity Types: OrderStatus and SalesOrder:

Figure 7. Result of Import from DDIC Structures – The two newly-created Entity Types: OrderStatus and SalesOrder


Create the Entity Sets for the entity types that you have just created.

Right-click the folder Entity Sets, and then select Create.

Figure 8. Create Entity Sets


Create an Entity Set SalesOrderSet for the entity type SalesOrder.

(A naming convention is to make the Entity Set name by appending ‘_Set’ to the Entity Type name.)

Figure 9. Create Entity Set SalesOrderSet for the entity type SalesOrder

Create an Entity Set OrderStatusSet for the entity type OrderStatus:

Figure 10. Create Entity Set OrderStatusSet for the entity type OrderStatus


You should now see the newly-created entity sets:

Figure 11. The newly-created Entity Sets


Create the Associations which sets the cardinal rules, and then click Next.

Figure 12. Create Association, step 1 of 3


Set the Dependent Property, Vbeln, and then click Next.

Figure 13. Create Association, step 2 of 3


Click Finish.

Figure 14. Create Association, step 3 of 3

Click Save.

Figure 15. Save the changes made

Check Consistency. Right-click ZORDER_MAINTAIN and then select Check Consistency:

Figure 16. Check Consistency

Generate runtime objects

To use the service with the changes that you have made, you need to generate the runtime objects. This creates the underlying ABAP classes that contain all the information that you've specified in the service builder. Click the Generate Runtime Objects icon or right-click ZORDER_MAINTAIN and then select Generate Runtime:

Figure 17. Generate Runtime Objects


While generating the runtime objects, the class and service details are generated as shown below.

Figure 18. Generate Runtime Objects – Model and Service Definition


Choose the package for the objects.

Figure 19. Generate Runtime Objects – Create Object Directory Entry


Now, you should see that the runtime objects were created successfully:

Figure 20. Messages confirming that the runtime objects were created


This completes the design stage. Next, you need to develop the code as part of the implementation.

Expand the Runtime Artifacts folder.

Figure 21. Expand the Runtime Artifacts folder


Double-click the Data Provider Class Extension (DPC_EXT)

Figure 22. Double-click the Data Provider Class Extension (DPC_EXT)


Implement the insert method

Because more than one structure or table is involved, you need to implement the method '/IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_DEEP_ENTITY' for implementing CREATE (POST) functionality for deep insert. However, for single insert, you can use the CREATE_ENTITY method.

To implement the method, first click the Change Mode icon:

Figure 23. Click the Change Mode icon


Select the method:

Deep Insert (Nested)

Figure 24. Select the Deep Insert method


Independent Insert Implement separate CREATE ENTITY methods as shown below:

Figure 25. Select the Independent Insert method


After selecting the method, click the Redefine icon.

Figure 26. Click the Redefine icon to redefine and edit the method


The method has been redefined and is available to code the business logic.

Add the business logic for your requirements.

Figure 27. Add business logic to the method

After completing the logic development, the CREATE functionality is complete.

Register the service Use Service Registration: Transaction: /IWFND/MAINT_SERVICE

In the Activate and Maintain Service window, click Add Service

Figure 28. Activate and Maintain Service window – Add Service


Get the Technical service name from the transaction SEGW.

The Runtime artifact which ends with 'SRV' in SEGW refers to the Technical Service; for example:

Figure 29. Example runtime artifact in SEGW that refers to the Technical Service


In the next screen, specify the System Alias (created by Basis) and the new service which has been created.

After that, click the Get Services icon. The result is displayed:

Figure 30. Result: New service has been added


Click Add Selected Services.

Our service is now registered and ready to be exposed in Fiori application. In the above steps, we saw how to create an entry in table using create OData service. Similarly, if we are to read, update or delete entries we can redefine respective methods and add appropriate code.

In the next blog, we will look deeply into how to register service SAP OData service through /iwfnd/maint_service




Read more