Simple Steps to Develop ODATA Using Global Classes

This blog will show you how to develop ODATA using Global Classes in CREATE_DEEP_ENTITY Method Operation with JSON Format using simple steps.
Step 1:
Create Custom Global Classes with below Structures.

Go To Transaction SE11 and create structures as displayed in the below screenshots.

Create Table Type ZPHYSINV_ITEMS_TAB.
Include the line type Structure.
ODATA display Table Type
Structure 1:
BAPI_PHYSINV_CREATE_ITEMS
ODATA dictionary display Structure

Structure 2:
BAPI_PHYSINV_CREATE_HEAD
ODATA dictionary display Structure 2

Structure 3:
Create Table Type ZRETURN_TAB.
Include the line type Structure ZBAPIRET.
ODATA dictionary display Table

ODATA dictionary display Structure

Go to Transaction SE24 and create a global class with following parameters.
Global Class: ZCL_MOBIM_ICC
ODATA Class Builder

Parameters for Method: CREATE INVENTORY_DOCUMENT
ODATA Class Builder Display

After creating structures and class in SE24 with above given parameters, paste the below Code into the method CREATE INVENTORY_DOCUMENT.

Code Snippet:
method create_inventory_document.
*–Data Declaration
data: ls_head type bapi_physinv_create_head,
ls_maxitems type am07m-maxpo value 300,
lt_item type standard table of bapi_physinv_create_items,
ls_item type bapi_physinv_create_items,
lt_return type standard table of bapiret2,
ls_return type bapiret2,
wa_return type zbapiret.

data: lv_error_detected(1) type c,
lv_text type bapi_msg,
lv_flag.

*–Pass the Header Details
ls_head-plant = im_header-plant.
ls_head-stge_loc = im_header-stge_loc.
ls_head-doc_date = im_header-doc_date.
ls_head-plan_date = im_header-plan_date.

*–Passing the Multiple Items
loop at im_item into ls_item.
append ls_item to lt_item.
endloop.

*–Create Inventory Documents
call function ‘BAPI_MATPHYSINV_CREATE_MULT’
exporting
head = ls_head
maxitems = ls_maxitems
tables
items = lt_item
return = lt_return.

*–Reading Succefully Messages
read table lt_return into ls_return with key type = ‘E’.
if sy-subrc ne 0.
call function ‘BAPI_TRANSACTION_COMMIT’
exporting
wait = ‘1’.
loop at lt_return into ls_return where type eq ‘S’.
call function ‘FORMAT_MESSAGE’
exporting
id = ls_return-id
lang = sy-langu
no = ls_return-number
v1 = ls_return-message_v1
v2 = ls_return-message_v2
v3 = ls_return-message_v3
v4 = ls_return-message_v4
importing
msg = lv_text
exceptions
not_found = 1
others = 2.
clear: wa_return.
wa_return-type = ‘S’.
wa_return-message = lv_text.
append wa_return to ex_return.
clear: ls_return, wa_return, lv_text .
endloop.
else.
lv_flag = ‘X’.
endif.

*–Reading Error Messages
if lv_flag eq ‘X’.
call function ‘BAPI_TRANSACTION_ROLLBACK’.
loop at lt_return into ls_return where type eq ‘E’.
call function ‘FORMAT_MESSAGE’
exporting
id = ls_return-id
lang = sy-langu
no = ls_return-number
v1 = ls_return-message_v1
v2 = ls_return-message_v2
v3 = ls_return-message_v3
v4 = ls_return-message_v4
importing
msg = lv_text
exceptions
not_found = 1
others = 2.
clear: wa_return.
wa_return-type = ‘E’.
wa_return-message = lv_text.
append wa_return to ex_return.
clear: ls_return, wa_return, lv_text.
endloop.
endif.
Endmethod.

Now you have created the Global Classes. Now let’s start developing ODATA in Net Weaver gateway Client SEGW for above Class.

Step 2: Create Project in SEGW

As I mentioned earlier we will implement the CREATE_DEEP_ENTITY for multiple postings with single request like one Header with multiple Line items.

In the example below, I will show posting with one Header with multiple Line items.
Create Entity Types, Entity Set, Association and Navigations as follows.

Create two Entity Types and Entity Sets.

Entity Type-1 – ICC_Header
Entity Type-2 – ICC_Items

Entity Set-1- ICC_HeaderSet
Entity Set-2- ICC_ItemsSet

ZMOBOIM ICC

Properties of ICC_Header

ODATA ICC Header

Properties of ICC_Items

ODATA ICC Items

Step 3:
Create Association and Navigation.
Create Associations as shown below.
Association – Assoc_ICCHeader_ICCItems
ODATA Association ICC Items

Create Navigation as shown below.

Navigation – ICC_Items

SAP NetWeaver Gateway Service Builder

Now generate runtime artifacts. Once generation is successful, you will get 4 classes. Two for Data provider classes and two for Model provider classes. Implement the code in Data provider class as shown below.

Double click on the Class ZCL_MOBOIM_ICC_DPC_EXT. Go to ABAP Workbench.

ODATA MOBOIM ICC DPC EXT
Start the Gateway client by calling transaction /IWFND/GW_CLIENT.
Enter the following URI to test your implementation.
Append $metatda to base service URI.

/sap/opu/odata/sap/ZMOBOIM_ICC_SRV/?$metadata

If everything works perfectly then HTTP response will be displayed as shown below with value 200.

SAP NetWeaver Gateway Client
Check service document append $format=json.

ODATA SAP NetWeaver Gateway Client

Step 4:
Implementing the CREATE_DEEP_ENTITY Method.

Right click and redefine the Create_Deep_Entity Method as shown in below Screenshot.

Create Deep Entity Method

Now double click on CREATE_DEEP_ENTITY Method. Paste the code below.
ODATA Repository Browser

Code Snippet:

method /iwbep/if_mgw_appl_srv_runtime~create_deep_entity.

*–Types Declaration for Inventory Cycle Document
types: ty_t_iccitem type standard table of zcl_zmoboim_icc_mpc=>ts_icc_items with default key.

*–Represents Inventory Cycle Document structure – header with one of more items
types: begin of ty_s_icc. “Deep Structure for Inventory Cycle Document
include type zcl_zmoboim_icc_mpc=>ts_icc_header.
types: icc_items type ty_t_iccitem, “Name Should be as Entity Type Name
end of ty_s_icc.

*–Data Declaration for Inventory Cycle Document
data: ls_icc type ty_s_icc,
ls_iccitem type zcl_zmoboim_icc_mpc=>ts_icc_items,
ls_header type bapi_physinv_create_head,
it_item type standard table of bapi_physinv_create_items,
wa_item type bapi_physinv_create_items,
wa_icc_items like line of ls_icc-icc_items.

constants: lc_iccitem type string value ‘ICC_Items’.
constants: lc_countitem type string value ‘Count_Items’.
constants: lc_diffitem type string value ‘PostDiff_Items’.

*–Data Declaration for Return Message
data: lv_compare_result type /iwbep/if_mgw_odata_expand=>ty_e_compare_result,
lt_return type zreturn_tab,
ls_return type zbapiret.

case iv_entity_name.
when ‘ICC_Header’.

*–Validate whether the current request including the inline data matches
lv_compare_result = io_expand->compare_to( lc_iccitem ).
*– Upon match, access data from IO_DATA_PROVIDER
io_data_provider->read_entry_data( importing es_data = ls_icc ).

*–Pass the Header Data
ls_header-plant = ls_icc-plant.
ls_header-stge_loc = ls_icc-stge_loc.
ls_header-doc_date = ls_icc-doc_date.
ls_header-plan_date = ls_icc-plan_date.

clear: ls_iccitem.

*–Pass the Item Data
loop at ls_icc-icc_items into ls_iccitem.
move-corresponding ls_iccitem to wa_item.
call function ‘CONVERSION_EXIT_MATN1_INPUT’
exporting
input = ls_iccitem-material
importing
output = wa_item-material.

append wa_item to it_item.
clear: wa_item, ls_iccitem.
endloop.

*–Create Invertory Cycle Counting (Custom Global Class)
call method zcl_moboim_icc=>create_inventory_document
exporting
im_header = ls_header
im_item = it_item
importing
ex_return = lt_return.

if lt_return is not initial.
loop at lt_return into ls_return .
move-corresponding ls_return to wa_icc_items.
modify ls_icc-icc_items from wa_icc_items index sy-tabix transporting type message.
endloop.
endif.

copy_data_to_ref(
exporting
is_data = ls_icc
changing
cr_data = er_deep_entity
).
clear ls_icc.

when others.
endcase.
Endmethod.

Step 5:
Create a Request Payload.

HTTP Request in JSON Format:

HTTP Method : POST
X-CSRF-Token : Fetch
Content-Type : application/json

JSON Format Payload:

{
“d”:{
“Plant”:”1100″,
“StorageLoc”:”1000″,
“DocDate”:”2015-07-21T00:00:00″,
“PlanDate”:”2015-07-21T00:00:00″,
“ICC_Items”:[
{
“Material”:”2144″,
“Batch”:” “,
“StockType”:” “,
“AltUnit”:” ”
},
{
“Material”:”2145″,
“Batch”:” “,
“StockType”:” “,
“AltUnit”:” ”
}
]
}
}

Step 6:
Post the data with JSON format.

ODATA with JSON format
Put external break point and ensure whether the values are fetched in the structure or not.

JSON format-external break point

JSON format-IT-ITEM

Come out from the debugging.
Records will be created and HTTP response value 201 will be created as displayed below.

ODATA HTTP response

That’s all. You’re done with it!!!

Find a right partner to transform your mobility space to a safe and productive one. Need assistance? write to us!