Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Mithun_Sharma
Explorer
Title

IDOC data extraction method for multiple segments using an RFC Function Module.

Introduction

In this blog, we will discuss the process of extracting the IDOC data from multiple segments and sending it to a target system using an RFC function module.


The RFC Function Module is a custom interface to send the IDOC data to another system.


To do so, we need to Identify the following objects:

  • Message Type.

  • IDOC Segments.

  • Fields of the IDOC Segments.

  • Partner Number of Sender 


Summary

An IDOC consists of three records: a control record, a data record, and a status record, and it is recognised by a message type in the system.


The control record contains information such as the IDOC type, message type, status, sender, and receiver, and it is stored in the EDIDC table.


The data record contains IDOC data for segments, which is stored in the EDID4 table.


The status record contains the processing status of the IDOC, and it is stored in the EDIDS table. E.g., the successful status of an inbound IDOC is 53.


A segment is a structure that consists of several fields containing data stored in the SDATA field of the EDID4 table.


We will use these tables to retrieve the IDOC data from its multiple segments and wrap it inside an RFC function module for sending it to a target system.


The message type used here is REMADV, and the segments are E1IDKU1 and E1EDK02.


The RFC Function module extracts the data from the EDID4 table for message type REMADV and E1IDKU1/E1EDK02 segments. The status and control records are fetched from the EDIDS and EDIDC tables. Finally, the data is passed to an export structure of the RFC with error messages, if any.

Step by Step process to extract the IDOC data from multiple segments using an RFC Function Module.

Step 1.  Create a structure with the fields that are required by the target system.


Image Description: Structure of the export parameter "EX_IDOC_DATA" of the RFC Function Module

Step 2. Create an RFC function module for extracting the IDOC data.


Image Description: Import Parameters of the RFC Function Module


Image Description: Export Parameters of the RFC Function Module

Step 3. Following is the sample code to be filled in the RFC Function Module 

 
FUNCTION ZFM_RFC_IDOC_DATA .

*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(IM_FROM_DAT) TYPE EDIDC-CREDAT
*" VALUE(IM_TO_DAT) TYPE EDIDC-CREDAT
*" VALUE(IM_SNDPRN) TYPE EDIDC-SNDPRN OPTIONAL
*" VALUE(IM_STATUS) TYPE EDIDC-STATUS
*" EXPORTING
*" VALUE(EX_IDOC_DATA) TYPE ZTT_IDOC_DATA
*" VALUE(EX_RETURN) TYPE BAPIRETURN
*"----------------------------------------------------------------------
*-----------------------------------------------------------------------*

*-----------------------------------------------------------------------*
*-----------------------------------------------------------------------*
* The routine extracts the IDOC data for the message type "REMADV" .
PERFORM get_idoc_s_data USING im_from_dat
im_to_dat
im_sndprn
im_status
CHANGING ex_idoc_data .
* The routine gets the error messages
PERFORM error_msg USING gv_rc CHANGING ex_return.
ENDFUNCTION.

TABLES : edidc,edid4,edids.
TYPES : BEGIN OF tt_e1idku1.
INCLUDE STRUCTURE e1idku1.
TYPES : END OF tt_e1idku1.

TYPES : BEGIN OF tt_e1edk02.
INCLUDE STRUCTURE e1edk02.
TYPES : END OF tt_e1edk02.

TYPES : BEGIN OF tt_idoc_data.
INCLUDE STRUCTURE zidoc_data.
TYPES : END OF tt_idoc_data.

DATA : gl_edid4 TYPE edid4 .
DATA : gt_edid4 TYPE STANDARD TABLE OF edid4.
DATA : gt_edids TYPE STANDARD TABLE OF edids.
DATA : gs_e1idku1 TYPE tt_e1idku1.
DATA : gs_e1edk02 TYPE tt_e1edk02.
DATA : gs_idoc_data TYPE tt_idoc_data.
DATA : gt_idoc_data TYPE STANDARD TABLE OF tt_idoc_data.
DATA : gt_edidc TYPE STANDARD TABLE OF edidc.
DATA : gv_cntr TYPE edids-countr.
DATA : gv_str TYPE string.
DATA : gv_rc TYPE sy-subrc.

CONSTANTS : c_mestyp TYPE edidc-mestyp VALUE 'REMADV'.
CONSTANTS : c_segnam1 TYPE edid4-segnam VALUE 'E1IDKU1'.
CONSTANTS : c_segnam2 TYPE edid4-segnam VALUE 'E1EDK02'.
RANGES: r_date FOR edidc-credat.

FIELD-SYMBOLS : <gf_edidc> LIKE LINE OF gt_edidc.
FIELD-SYMBOLS : <gf_edid4> LIKE LINE OF gt_edid4.
FIELD-SYMBOLS : <gf_edids> LIKE LINE OF gt_edids.

*&---------------------------------------------------------------------*
*& Form GET_IDOC_S_DATA
*&---------------------------------------------------------------------*
* Get the IDOC data
*----------------------------------------------------------------------*
* -->P_IM_FROM_DAT text
* -->P_IM_TO_DAT text
* -->P_IM_SNDPRN text
* -->P_IM_STATUS text
* <--P_EX_IDOC_DATA text
*----------------------------------------------------------------------*
FORM get_idoc_s_data USING p_im_credat_from
p_im_credat_to
p_im_sndprn
p_im_status
CHANGING p_ex_idoc_data TYPE ztt_idoc_data .
CLEAR gv_rc.
* Build date range for EDIDC selection
PERFORM build_date_range USING p_im_credat_from
p_im_credat_to.
* Get the IDOC Data from EDIDC,EDID4 & EDIDS tables
PERFORM get_idoc_data TABLES gt_idoc_data
USING p_im_status
p_im_sndprn.
* Pass the final data to the exporting table
IF gv_rc EQ 0.
p_ex_idoc_data[] = gt_idoc_data[].
ENDIF.
ENDFORM.



*&---------------------------------------------------------------------*
*& Form BUILD_DATE_RANGE
*&---------------------------------------------------------------------*
* Build the date range
*----------------------------------------------------------------------*
* -->P_P_IM_CREDAT_FROM text
* -->P_P_IM_CREDAT_TO text
*----------------------------------------------------------------------*
FORM build_date_range USING p_im_credat_from
p_im_credat_to.
FREE r_date.
r_date-sign = 'I'.
r_date-option = 'BT'.
r_date-low = p_im_credat_from.
r_date-high = p_im_credat_to.
APPEND r_date.
ENDFORM.



*&---------------------------------------------------------------------*
*& Form GET_IDOC_DATA
*&---------------------------------------------------------------------*
* Get the IDOC data
*----------------------------------------------------------------------*
* -->P_GT_IDOC_DATA text
* -->P_P_IM_STATUS text
* -->P_P_IM_SNDPRN text
*----------------------------------------------------------------------*
FORM get_idoc_data TABLES gt_idoc_data
USING p_im_status
p_im_sndprn.
FREE gt_idoc_data.
FREE gt_edidc.
* Get the IDOC data from the EDIDC table
SELECT status docnum mestyp sndprn credat cretim idoctp
FROM edidc INTO CORRESPONDING FIELDS OF TABLE gt_edidc
WHERE status EQ p_im_status
AND credat IN r_date
AND sndprn EQ p_im_sndprn
AND mestyp EQ c_mestyp.
IF sy-subrc EQ 0.
IF gt_edidc[] IS NOT INITIAL.
UNASSIGN <gf_edidc>.
LOOP AT gt_edidc ASSIGNING <gf_edidc>.
CLEAR gs_idoc_data.
gs_idoc_data-docnum = <gf_edidc>-docnum.
gs_idoc_data-status = <gf_edidc>-status.
gs_idoc_data-mestyp = <gf_edidc>-mestyp.
gs_idoc_data-sndprn = <gf_edidc>-sndprn.
gs_idoc_data-credat = <gf_edidc>-credat.
gs_idoc_data-cretim = <gf_edidc>-cretim.
gs_idoc_data-idoctp = <gf_edidc>-idoctp.
* Get the IDOC data of E1IDKU1 from EDID4 table
PERFORM get_idoc_segment_data USING c_segnam1
CHANGING gs_idoc_data.

* Get the IDOC data of E1EDK02 from EDID4 table
PERFORM get_idoc_segment_data USING c_segnam2
CHANGING gs_idoc_data.
* Append the IDOC data to the final internal table
APPEND gs_idoc_data TO gt_idoc_data.
ENDLOOP.
ENDIF.
ELSE.
gv_rc = 1.
ENDIF.
IF gt_idoc_data[] IS NOT INITIAL.
gv_rc = 0.
ELSE.
gv_rc = 1.
ENDIF.
ENDFORM.



*&---------------------------------------------------------------------*
*& Form GET_IDOC_SEGMENT_DATA
*&---------------------------------------------------------------------*
* Get the IDOC segment data
*----------------------------------------------------------------------*
* -->P_C_SEGNAM1 text
* <--P_GS_IDOC_DATA text
*----------------------------------------------------------------------*
FORM get_idoc_segment_data USING p_segnam
CHANGING gs_idoc_data .
* Get the IDOC segment data from the EDID4 table
FREE gt_edid4.
SELECT * FROM edid4 UP TO 1 ROWS INTO CORRESPONDING FIELDS OF TABLE gt_edid4
WHERE docnum EQ <gf_edidc>-docnum AND segnam EQ p_segnam.
IF sy-subrc EQ 0.
IF gt_edid4[] IS NOT INITIAL.
UNASSIGN <gf_edid4>.
LOOP AT gt_edid4 ASSIGNING <gf_edid4>.
CASE p_segnam.
WHEN 'E1IDKU1'.
CLEAR gs_e1idku1.
gs_e1idku1 = <gf_edid4>-sdata.
MOVE-CORRESPONDING gs_e1idku1 TO gs_idoc_data.
WHEN 'E1EDK02'.
CLEAR gs_e1edk02.
gs_e1edk02 = <gf_edid4>-sdata.
MOVE-CORRESPONDING gs_e1edk02 TO gs_idoc_data.
WHEN OTHERS.
EXIT.
ENDCASE.
* Get the IDOC Status Text from the EDIDS table
PERFORM get_idoc_status_text CHANGING gs_idoc_data.
ENDLOOP.
ENDIF.
ENDIF.
ENDFORM.

*&---------------------------------------------------------------------*
*& Form GET_IDOC_STATUS_TEXT
*&---------------------------------------------------------------------*
* Get the IDOC status text
*----------------------------------------------------------------------*
* <--P_GS_IDOC_DATA text
*----------------------------------------------------------------------*
FORM get_idoc_status_text CHANGING p_gs_idoc_data TYPE tt_idoc_data.
* Get the last counter value from the EDIDS table
CLEAR gv_cntr.
SELECT MAX( countr ) INTO gv_cntr FROM edids
WHERE docnum EQ <gf_edid4>-docnum.
IF sy-subrc EQ 0.
* Get the IDOC status text from the EDIDS table
FREE gt_edids.
SELECT * FROM edids UP TO 1 ROWS INTO CORRESPONDING FIELDS OF TABLE gt_edids
WHERE docnum EQ <gf_edid4>-docnum
AND countr EQ gv_cntr.
IF sy-subrc EQ 0.
UNASSIGN <gf_edids>.
LOOP AT gt_edids ASSIGNING <gf_edids>.
CLEAR gv_str.
CONCATENATE <gf_edids>-stapa1 <gf_edids>-stapa2
<gf_edids>-stapa3 <gf_edids>-stapa4 INTO gv_str SEPARATED BY space.
REPLACE ALL OCCURRENCES OF '&'IN <gf_edids>-statxt WITH gv_str.
gs_idoc_data-statxt = <gf_edids>-statxt.
ENDLOOP.
ENDIF.
ENDIF.
ENDFORM.

*&---------------------------------------------------------------------*
*& Form Error_MSG
*&---------------------------------------------------------------------*
* Get the error messages
*----------------------------------------------------------------------*
* -->P_EX_IDOC_DATA text
* <--P_EX_RETURN text
*----------------------------------------------------------------------*
FORM error_msg USING p_gv_rc TYPE sy-subrc
CHANGING p_ex_return TYPE bapireturn.
IF p_gv_rc EQ 0.
p_ex_return-type = 'S'.
p_ex_return-code = '001'.
p_ex_return-message = 'Success.'.
ELSE.
p_ex_return-type = 'E'.
p_ex_return-code = '002'.
p_ex_return-message = 'Error.'.
ENDIF.
ENDFORM.

Step 4. Execute the RFC Function Module.


Image description: Input Parameters of the RFC Function module.

Output:


Image description: Output of the RFC Function module.

Conclusion

The blog provides a simple method for businesses to track the status of their IDOCs and act as an effective tool to mitigate failures, thus improving the robustness of the system.

I hope this article was insightful.

Any comments or suggestions will be appreciated.

 

Pls follow the following pages for SAP ABAP Development and post your answer & questions:

ABAP Development environment Topic Page:

https://community.sap.com/topics/abap

Post and answer questions:

https://answers.sap.com/tags/833755570260738661924709785639136

and read other posts on the topic:

https://blogs.sap.com/tags/833755570260738661924709785639136

My profile

Please follow my profile for future posts  minku.pinku

 

Thank you.