SAP Process Order Idoc Outound – SAP IDOC LOIPRO (Sample ABAP)

SAP Process Order

SAP Process Order Idoc Outound: Sample ABAP is the last part of LOIPRO IDoc.

It will covers a sample ABAP code how to generate LOIPRO IDoc including preparing Data and calling the CLOI* BAPI to create a LOIPRO Idoc.

SAP Process Order Idoc Outound: Trigger LOIPRO IDoc when Save

Check more detail on How To trigger IDOC LOIPRO when PO Changed/Created.

The BADI to be implemented is WORKORDER_UPDATE and the method is AT_SAVE

METHOD if_ex_workorder_update~at_save.
  " SAP Process Order Idoc Outound when PO/WO Saved
            message_type = 'ZLOIPRO'
            aufnr        = is_header_dialog-aufnr
            werks        = is_header_dialog-werks
            matnr        = is_header_dialog-matnr.


SAP Process Order Idoc Outound: Prepare Data

Before calling the standard function CLOI_MASTERIDOC_CREATE_LOIPRO to create the LOIPRO01 IDoc for SAP Process Order Master, some structure must be filled.

Get here How to Get SAP Process Order Status

Here a sample ABAP code to fill the ORDER_DATA TYPE CLOI_AFKO_TAB

  DATA: lt_order_data TYPE  cloi_afko_tab.
  DATA: ls_order_data TYPE  cloi_afko_struct.
*  " Fill Order Data for SAP Process Order Idoc Outound
  TYPES: BEGIN OF ty_jest,
     stat LIKE jest-stat ,
     END OF ty_jest.

  " Retrieve PO Master Data
  DATA: ls_afko TYPE afko.
  DATA: ls_aufk TYPE aufk.
  DATA: lt_afpo TYPE TABLE OF afpo.
  DATA: lt_jest TYPE TABLE OF ty_jest.
  DATA: ls_jest TYPE ty_jest.
  DATA: lt_affl TYPE TABLE OF affl.
  DATA: ls_affl TYPE affl.
  DATA: lt_afab TYPE TABLE OF afab.
  DATA: ls_afab TYPE afab.
  DATA: lt_afvv TYPE TABLE OF afvv.
  DATA: ls_afvv TYPE afvv.

  " Tables and structures for BAPI mapping
  DATA: lt_jstk TYPE cloi_jstk_tab.
  DATA: ls_jstk LIKE LINE OF lt_jstk.
  DATA: lt_affl_tab TYPE cloi_affl_tab.
  DATA: ls_affl_str TYPE cloi_affl_struct.
  DATA: lt_afab_tab TYPE cloi_afab_tab.
  DATA: ls_afab_str TYPE cloi_afab_struct.
  DATA: lt_afvv_tab TYPE cloi_afvo_tab.
  DATA: ls_afvv_str TYPE cloi_afvo_struct.

* AFKO: Order Header Data PP Orders
  SELECT SINGLE * FROM afko INTO ls_afko
  WHERE aufnr = aufnr.

* AUFK: Order master data
  SELECT SINGLE * FROM aufk INTO ls_aufk.

* JEST: Work Order Status 
  SELECT  stat  FROM jest INTO TABLE lt_jest
  WHERE objnr = ls_aufk-objnr.

  SELECT * FROM afpo INTO TABLE lt_afpo
   WHERE aufnr = aufnr.

* AFFL:  Work order sequence
  SELECT * FROM affl INTO TABLE lt_affl
  WHERE aufpl = ls_afko-aufpl.

* AFVV:DB structure of the quantities/dates/values in the operation
  IF NOT lt_affl[] IS INITIAL.
    SELECT * FROM afvv INTO TABLE lt_afvv
     FOR ALL ENTRIES IN lt_affl
     WHERE aufpl = lt_affl-aufpl
      AND  aplzl = lt_affl-aplzl.

* Fill Data to BAPI input
  MOVE-CORRESPONDING ls_afko TO ls_order_data.
  MOVE-CORRESPONDING ls_aufk TO ls_order_data.
  ls_order_data-objnr = ls_aufk-objnr.
  LOOP AT lt_jest INTO ls_jest.
    CLEAR ls_jstk.
    ls_jstk-stat = ls_jest-stat.
    APPEND ls_jstk TO lt_jstk.

  LOOP AT lt_affl INTO ls_affl.
    CLEAR ls_affl_str. REFRESH lt_afvv_tab.
    MOVE-CORRESPONDING ls_affl TO ls_affl_str.
    LOOP AT lt_afvv INTO ls_afvv
     WHERE aufpl = ls_affl-aufpl
      AND  aplzl = ls_affl-aplzl.
      CLEAR ls_afvv_str.
      MOVE-CORRESPONDING ls_afvv TO ls_afvv_str .
      APPEND ls_afvv_str TO lt_afvv_tab.
    ls_affl_str-t_afvo[] = lt_afvv_tab[].

    APPEND ls_affl_str TO lt_affl_tab.

  LOOP AT lt_afab INTO ls_afab.
    CLEAR ls_afab_str.
    MOVE-CORRESPONDING ls_afab TO ls_afab_str.
    APPEND ls_afab_str TO lt_afab_tab.

  ls_order_data-t_afpo = lt_afpo[].
  ls_order_data-t_jstk = lt_jstk[].
  ls_order_data-t_affl = lt_affl_tab[].
  ls_order_data-t_afab = lt_afab_tab[].

  APPEND ls_order_data TO lt_order_data.

Here an other interesting SCN article Creation of production order IDOCs to a given selection

SAP Process Order Idoc Outound: Create LOIPRO IDOC

Once the table ORDER_DATA is populated, the CLOI_MASTERIDOC_CREATE_LOIPRO can be called to generate the SAP Work Order Idoc ( LOIPRO01).

" Call the SAP Process Order Idoc Outound IDOC LOIPRO01 Creation
            opt_sys      = lv_logsys
            message_type = message_type
            order_data   = lt_order_data.

You may also like:

Check also this article on Automation of LOIPRO outbound IDOCS on