Skip to Content
Author's profile photo Vijey Rajendran

Simple Steps to Develop ODATA Using Global Classes in CREATE_DEEP_ENTITY Method Operation with JSON Format

Simple-Steps-to-Develop-ODATA-Using-Global-Classes-in-CREATE_DEEP_ENTITY-Method-Operation-with-JSON-Format.jpg

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.png

Structure 1:

BAPI_PHYSINV_CREATE_ITEMS

ODATA-dictionary-display-Structure.png

Structure 2:

BAPI_PHYSINV_CREATE_HEAD
ODATA-dictionary-display-Structure-2.png

Structure 3:

Create Table Type ZRETURN_TAB.

Include the line type Structure ZBAPIRET.

ODATA-dictionary-display-Table.png

ODATA dictionary display Structure.png

Go to Transaction SE24 and create a global class with following parameters.

Global Class: ZCL_MOBIM_ICC

ODATA-Class-Builder.png

Parameters for Method: CREATE INVENTORY_DOCUMENT

ODATA Class Builder-Display.png

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.

  1. 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.png

Properties of ICC_Header

ODATA-ICC-Header.png

Properties of ICC_Items

ODATA-ICC-Items.png

Step 3:

Create Association and Navigation.

Create Associations as shown below.

Association – Assoc_ICCHeader_ICCItems

ODATA-Association-ICC-Items.png

Create Navigation  as shown below.

Navigation – ICC_Items

SAP-NetWeaver-Gateway-Service-Builder.png

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.png

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.png

Check service document append $format=json.

ODATA SAP NetWeaver Gateway Client.png

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.png

Now double click on CREATE_DEEP_ENTITY Method. Paste the code below.

ODATA Repository Browser.png

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.

  1. 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.png

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

JSON format-external break point.png

JSON format-IT-ITEM.png

Come out from the debugging.

Records will be created and HTTP response value 201 will be created as displayed below.

ODATA HTTP response.png

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

I know the above steps will be useful in developing ODATA with Global Classes in Net Weaver gateway. Suggestions and comments for this post will be appreciated.

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi Vijay,

      BAPI_PHYSINV_CREATE_ITEMS is not available on SAP system.

      Kindly suggest.


      Regards

      Saket

      Author's profile photo Former Member
      Former Member

      Hi Saket,

      What is the version of teh SAP_APPL component in your landscape.

      Thanks and Regards,
      Neerup.

      Author's profile photo Former Member
      Former Member

      Hi Nirup,

      The SAP_APPL version is as Release 617 SP 0008.

      SAP_APPL.JPG

      Regards

      Saket

      Author's profile photo Former Member
      Former Member

      Hi Saket,

      BAPI_PHYSINV_CREATE_ITEMS  is not an BAPI its a structure. Check it in SE11.


      Regards

      Shakeel

      Author's profile photo Former Member
      Former Member

      Capture.PNG

      I am on SAP_GWFND - 7.40 - 0010, (Component - Release - SP-Level). When I am posting the data with above json format.. I am getting response as --> "Error while parsing an XLM Stream. I am using same CSRF token and Content type...

      Please help