Rest Based Api

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Muhammad Uzair (SAP Technical Consultant ABAP, BTP, CPI & SAC) m.uzairkoreja@gmail.

com

REST BASED API in SAP ABAP


Scenario:
We will be fetching details of sale order from SAP by passing Sale Order Number (VBELN)
using SAP ABAP Created REST API. The API will be triggered from Third Party tool to fetch
Sale Order Details.
We will create a REST API in SAP System in ABAP and Test it from POSTMAN.

Development Design:
We must fetch Header Information Line Items Information along Condition values.
Elements to develop.

1) We will create deep structure first.


2) Create Table types for structures.
3) Create Resource Provider Class.
4) Create Resource Handler Class.
5) Create SICF Node.
6) Implement method for getting sale order details.
7) Test Service from POSTMAN

1
Muhammad Uzair (SAP Technical Consultant ABAP, BTP, CPI & SAC) [email protected]

Step 1: Create Structures and Table Types


Note: Below Structure there are table types are given. I will recommend creating a table type
of related structure just after creating its structure.

Structures:
1. ZSD_ITEM_COND

Quantity Field Reference:

2
Muhammad Uzair (SAP Technical Consultant ABAP, BTP, CPI & SAC) [email protected]

2. ZSD_ITEMS

Quantity Field Reference:

3
Muhammad Uzair (SAP Technical Consultant ABAP, BTP, CPI & SAC) [email protected]

3. ZSD_SALEORDERS

4
Muhammad Uzair (SAP Technical Consultant ABAP, BTP, CPI & SAC) [email protected]

Table Types:
1. ZTT_SD_ITEM_COND

2. ZTT_SD_ITEMS

5
Muhammad Uzair (SAP Technical Consultant ABAP, BTP, CPI & SAC) [email protected]

Step 2: Create Resource Provider Class


Class Name: ZCL_SD_RP
Go to SE24 write class name and click Create Button.
Note: Superclass name will be as it is.

This Class will be created:


1) Click on Get Method and Click on Redefine Method Button:

6
Muhammad Uzair (SAP Technical Consultant ABAP, BTP, CPI & SAC) [email protected]

The new window will appear to add code, For now just save and activate.

7
Muhammad Uzair (SAP Technical Consultant ABAP, BTP, CPI & SAC) [email protected]

Step 3: Create Resource Handler Class


Class Name: ZCL_SD_RH
Go to SE24 write class name and click Create Button.
Note: Superclass name will be as it is.

1) First Redefine the GET_ROOT_HANDLER Method:

8
Muhammad Uzair (SAP Technical Consultant ABAP, BTP, CPI & SAC) [email protected]

Code For GET_ROOT_HANDLER Method:


You can just copy and paste.
Note:
• ‘/SO’ Is just like a resource library. This will be used in HTTP Link after method.
• ‘ZCL_SD_RP’ Is the resource provide class name.

DATA(LO_ROUTER) = NEW CL_REST_ROUTER( ).

LO_ROUTER->ATTACH(
EXPORTING
IV_TEMPLATE = '/SO' " Unified Name for Resources
IV_HANDLER_CLASS = 'ZCL_SD_RP' " Object Type Name
).

ro_root_handler = lo_router.

2) Now Redefine the HANDLE_CSRF_TOKEN method.

Note: Just save it and activate the Method and Class Also.

9
Muhammad Uzair (SAP Technical Consultant ABAP, BTP, CPI & SAC) [email protected]

Step 4: Definition and Create SICF Node


1) Definition:
Whey We Create SICF Node:
In SAP ABAP, a SICF (SAP Internet Communication Framework) node is created to
define a specific entry point for external clients to communicate with the ABAP system using
HTTP or HTTPS protocols.

Exposing ABAP Functionality as a RESTful Service:


By creating a SICF node, you can expose the functionality of your ABAP program as a
RESTful service. This allows external clients, such as mobile applications, web applications,
or other systems, to interact with your ABAP system using standard HTTP requests and
responses.

2) Create SICF Node:


Goto SICF T-Code:
• Simply Click on default_host
• Click on Create Host/Service Button

10
Muhammad Uzair (SAP Technical Consultant ABAP, BTP, CPI & SAC) [email protected]

Give the Service Name and Hit OK.

Fill these Information on Logon Data:


Note: Must Enter Some Description in Description 1.

Just Save and Go Back.

11
Muhammad Uzair (SAP Technical Consultant ABAP, BTP, CPI & SAC) [email protected]

Click on handler List tab and Give Handler Class Name:

It will look written in Gray Colored Text.


Its gray because we have not activated the Service.

12
Muhammad Uzair (SAP Technical Consultant ABAP, BTP, CPI & SAC) [email protected]

Just Right Click on Service name and Click on Activate Service:

There is another button to activate service:

13
Muhammad Uzair (SAP Technical Consultant ABAP, BTP, CPI & SAC) [email protected]

Now you can see its name color is black.

Step 5: Define Get method of Resource Provider Class


ZCL_SD_RP
• Go to SE24 T-Code and open your Resource Provider Class.
• Double click on IF_REST_RESOURCE~GET method.
• Copy and paste the below code for get method.

Note: Must change the Structures name if created with your own names. If they are same then
remain the code as it is.

DATA : GS_SO TYPE ZSD_SALESORDERS,


LV_RES TYPE STRING,
LV_VBELN TYPE VBELN.

DATA : GS_ITEM_CONDITION TYPE ZSD_ITEM_COND,


GT_ITEM_CONDITION TYPE STANDARD TABLE OF ZSD_ITEM_COND,
GS_ITEM TYPE ZSD_ITEMS,
GT_ITEM TYPE STANDARD TABLE OF ZSD_ITEMS.

"Read the passing paramters


LV_VBELN = MO_REQUEST->GET_URI_QUERY_PARAMETER( IV_NAME = 'VBELN' ).
IF LV_VBELN IS NOT INITIAL.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = LV_VBELN
IMPORTING
OUTPUT = LV_VBELN.

"select header data


SELECT SINGLE * FROM VBAK INTO @DATA(GS_VBAK) WHERE VBELN = @LV_VBELN
.
IF GS_VBAK-VBELN IS NOT INITIAL.

"select line item data


SELECT * FROM VBAP INTO TABLE @DATA(GT_VBAP) WHERE VBELN = @GS_VBAK
-VBELN.

"select conditions
SELECT * FROM KONV INTO TABLE @DATA(GT_KONV) WHERE KNUMV = @GS_VBAK
-KNUMV.

"pass the data to structure.


GS_SO-VBELN = GS_VBAK-VBELN.
GS_SO-KUNNR = GS_VBAK-KUNNR.

GS_SO-VKORG = GS_VBAK-VKORG.
GS_SO-VTWEG = GS_VBAK-VTWEG.
GS_SO-SPART = GS_VBAK-SPART.
GS_SO-BSTNK = GS_VBAK-BSTNK.

14
Muhammad Uzair (SAP Technical Consultant ABAP, BTP, CPI & SAC) [email protected]

GS_SO-BSTDK = GS_VBAK-BSTDK.

* ITEMS
LOOP AT GT_VBAP INTO DATA(GS_VBAP).

CLEAR : GS_ITEM.
MOVE-CORRESPONDING GS_VBAP TO GS_ITEM.

LOOP AT GT_KONV INTO DATA(GS_KONV) WHERE KNUMV = GS_VBAK-KNUMV


AND KPOSN = GS_VBAP-POSNR.

CLEAR : GS_ITEM_CONDITION.
GS_ITEM_CONDITION-VBELN = GS_VBAP-VBELN.
GS_ITEM_CONDITION-POSNR = GS_VBAP-POSNR.
GS_ITEM_CONDITION-KPOSN = GS_KONV-KPOSN.
GS_ITEM_CONDITION-STUNR = GS_KONV-STUNR.
GS_ITEM_CONDITION-KSCHL = GS_KONV-KSCHL.
GS_ITEM_CONDITION-KWERT = GS_KONV-KWERT.

APPEND GS_ITEM_CONDITION TO GT_ITEM_CONDITION.

CLEAR : GS_KONV, GS_ITEM_CONDITION.


ENDLOOP.

GS_ITEM-ITEM_CONDITIONS[] = GT_ITEM_CONDITION[].
APPEND GS_ITEM TO GT_ITEM.

CLEAR : GS_VBAP, GT_ITEM_CONDITION[].


ENDLOOP.

GS_SO-ITEMS[] = GT_ITEM[].

/UI2/CL_JSON=>SERIALIZE( EXPORTING
DATA = GS_SO " Data to serialize
RECEIVING
R_JSON = LV_RES " JSON string
).

MO_RESPONSE->CREATE_ENTITY( )->SET_STRING_DATA( IV_DATA = LV_RES ).

MO_RESPONSE->SET_HEADER_FIELD(
EXPORTING
IV_NAME = 'Content-Type' " Header Name
IV_VALUE = 'application/json' " Header Value
).

ENDIF.
ELSE.
ENDIF.

Save and Activate method and Class.

15
Muhammad Uzair (SAP Technical Consultant ABAP, BTP, CPI & SAC) [email protected]

Step 6: Test the Service Using POSTMAN.

Service Creation:
Your Service will be like this.
http:// Server link : Host /Service name /Resource Directory ?Landscape
Client &Parameter

Example:
http://:8000/zsd_rest/SO?sap-client=100&VBELN=20000014

• Go to POSTMAN and create new collection.


• Give the collection name.

16
Muhammad Uzair (SAP Technical Consultant ABAP, BTP, CPI & SAC) [email protected]

17

You might also like