Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

API - S/4 HANA or On-premises

In 2016 SAP launched API Business Hub which is the central catalogue of SAP APIs for
application developers to search and discover standard and out of the box ways of
accessing/integrating SAP data with third-party systems or applications.

S/4HANA API is a concept somehow like BAPIs known from SAP ECC systems. The
difference here is that in the S/4HANA they are RESTful APIs and use the OData protocol,
whereas BAPIs were mainly called using RFC connection (if called from outside of SAP).
Also, under the hood, S/4HANA APIs are built using CDS views as opposed to regular
ABAP.

Having said that, you can leverage the APIs to quickly build reports without the need of
developing the backend and database logic. That is the case if the API you need is available
in the API Business Hub and provides all the data you need. Sometimes, you may find that
the API doesn’t provide all the information you need, but fear not, SAP got you covered and
allows you to extend the API and add additional fields.

System Prerequisites: S/4HANA System, Eclipse with ABAP Development Tools


There are 2 types of APIs.

 ODATA API
 SOAP API

1.Business Scenario - Extending Standard released API with new fields.

Where to find the SAP API?

The API Business Hub can be accessed using the following URL: https://api.sap.com/.
Searching for “Purchase Order” we have found a relevant API –
“API_PURCHASEORDER_PROCESS_SRV”. You can then check if this API is available in
our system version and activate it.
This API is also available in S/4HANA on premise version and SAP S/4HANA Cloud.

ODATA V2/V4 API-Are OData based API and can be added directly from Tcode -
/N/IWFND/MAINT_SERVICE if not available.

Search and activate the required service and execute to see the status.
How to identify the relevant object for enhancement?

As already mentioned, most of the APIs are created using CDS views, which are then
exposed using the OData protocol to the outside world. To add the extra field, you must
identify the APIs Gateway project and then the relevant CDS view name. It is the CDS view
that is enhanced directly and the API, which is created with reference to the CDS view will
inherit the enhancement.

1. Launch transaction SEGW

2. Click the ‘Open Project’ button and type API_PRUCHASEORDER_PROCESS

3. Expand the Gateway project and click on “CDS-Entity Exposures”. This will display on the
right-hand side all CDS views used in that API. We are interested in A_PurchaseOrder (PO
Header Level) and this is the CDS view we will be enhancing.
Extending the CDS view

We can now extend the CDS view using Eclipse. This is done by creating a new “Data
Definition” element and the “Extend View” template.

1. Launch Eclipse and connect to your system.

2. Click File -> New -> Other -> Data Definition

3. Enter Name and Description, click next and select transport.

4. Select ‘Extend View’ Template.


5. Add the additional "Building" or use association to get a new field.

6. Activate your extension

7. Launch the A_PurchaseOrder CDS view. Note that a new icon has appeared which
indicates that it has been enhanced, and shows the enhancement name:

Test your SAP APIs

Now it is time to validate that your new fields are visible and providing the correct data.

1. Check if the new fields have appeared in transaction SEGW for project:
API_PRUCHASEORDER_PROCESS, entity type: A_PURCHASEORDERTYPE, -node:
properties
2. Test your service in the browser or SAP Gateway Client in transaction
/IWFND/MAINT_SERVICE

Using request URI:


/sap/opu/odata/sap/API_PURCHASEORDER_PROCESS_SRV/A_PurchaseOrder

We can now see that the newly added fields are appearing:

Right now, in current system I have limited access and data is not maintained in Sandbox so
"No show of data". However new added field is visible in Entity set of OData.

2. Business Scenario - No convenient released API is available to create purchase


requisitions, so documentation recommends using a BAPI as an alternative. Creating wrapper
API for unreleased or released BAPI in system and release the wrapper API for consumption in
tier 1.

How to check if BAPI is released or not?

In Eclipse Editor, Under Properties check the API state.


Create a wrapper interface

You now want to wrap the API BAPI_PR_CREATE . Depending on your specific use-case, you

normally would need to access only certain specific functionalities and methods of the BAPI you want
to expose. An ABAP Interface is the perfect development object for this purpose: the interface
simplifies and restricts the usage of the underlying BAPI for the specific use-case, by exposing only
the parameters that are needed. Therefore, non-wrapped functionalities are forbidden.

To create the interface for your BAPI wrapper right click on the newly created package and
select New > ABAP Interface. Input the Name ZIF_WRAP_BAPI_PR_CREATE_XXX and a

Description.

Click on Next and then click on Finish.

Implement your ABAP Interface to expose only the parameters that are needed in your specific use-
case. Save and activate it. As already said, you will expose only the parameters that are needed in
your specific use-case. In this case you want to create a purchase requisition item (for which you
expose the parameter PR_ITEM ) and given the underlying BAPI signature, the PR_ITEM always
requires a header, which is why you are also exposing the parameter PR_HEADER.
Create a wrapper class

You now need to create a class to wrap the BAPI (implementing the interface you created in the
previous step) and implement its methods.

Right click on your package and select New > ABAP Class. Input the
Name ZCL_BAPI_PR_WRAPPER_XXX and a Description:
Click on Next and then click on Finish.

The implementation of the wrapper class depends on the specific use-case and BAPI signature. We
suggest implementing the wrapper class in the following way:

The wrapper class has a method defined in the private section, CALL_BAPI_PR_CREATE , which

has access to all the parameters of the underlying BAPI. Having this type of central private method is
best practice: internally, the wrapper class has access to all the parameters and then the interface has
virtual access to all of these parameters and exposes publicly only the ones that are needed
depending on the specific use-case. Save and activate it.

Since we plan to access the wrapped BAPI in a different tier, it is good to provide the possibility to test
it, and to keep wrapping-specific coding in tier 1 to a minimum. For this reason, the interface approach
is recommended, and the wrapper class will not be released directly for consumption in tier 1, but
rather will be accessible via a factory class that you will create in the next step.

Create a wrapper factory class

Our recommended approach is to create a factory class to control the instantiation of the wrapper
class. This factory class will then be released for consumption in tier 1. This approach has the
advantage of a clear control of when and where an instance of the wrapper class is created, and in
the event in which several wrapper classes are needed all their instantiations could be handled inside
one single factory class. Also, in case of wrapper classes this has the advantage that in case the
wrapper class is changed throughout its software lifecycle, at a later point in time a different class
could be initialized, without changes to the consumer implementation.

To create the factory class right click on your package and select New > ABAP Class. Input the
Name ZCL_BAPI_WRAP_FACTORY_XXX and a Description. Save and activate it.
Release the wrapper interface and factory class

Now you need to release the wrapper interface and wrapper factory class for consumption in tier 1. To
do this, you need to add a Release Contract (C1) to both objects for use system-internally and use in
Cloud Development.

In your Project Explorer open the ABAP Interface you created. In the Properties tab click on the API
State tab and then click on the green plus icon next to the Use System-Internally (Contract C1).

Make sure the Release State is set to Released and check the option Use in Cloud Development:

The API State tab will now show the new Release State:
In ADT, now you can create the class for the console application. Right click on the newly created
package and select New > ABAP Class and input the Name ZCL_BAPI_WRAP_TEST_XXX and a

Description:
You can test that the wrapper was correctly released for consumption in tier 1 by running the console
application class ZCL_BAPI_WRAP_TEST_XXX . First, the errors in the class should have

disappeared now that you released the wrapper, so you can save and activate the class. Now you can
run it: right click on the class and select Run As > ABAP Application (Console). The class should now
run without errors and the purchase requisition will be created and displayed in the console:

You might also like