Sending idoc from production to test system

This topic comes to me once in a while, but since I usually need one or two idocs it is always quicker to do the saving and downloading via we19 and standard programs. However lately there came a request – We need to transfer 100 idocs from one system to another, to discover a defect…

It's a SAP technology again, so like with reduction topic, of course there is a well documented standard approach...

If you needed to recreate a test subject fromm a production environment, you sometimes need to transfer the idoc from one system to another, because “it’s not possible” to do it from the 3rd party system. Of course you most likely seen the approach of saving the idoc to file, downloading it locally and uploading to correct system [1], however it’s not a viable option for the mass idoc transfer. Another approach is if you own PO/SIS connected to that system and the idocs are going through it, you can always download it and than pass through another channel to the right system, however this is also a manual way and a no go for bigger amount.

If we think about the standard automation tools like lsmw or migration cockpit this should also be possible though its not cost efficient. Why? Simply because you would need to define route for each idoc, with mappings (recordings are even worse). On the other hand in the migration cockpit, you could most likely synchronize the tables for idocs, but to tell the truth you could do the same with sap edit and then you would still need to reprocess them manually, which is not a big help.

So actually why there is no report that would allow to select idocs and resend them to another system? Actually even though SAP have all of the tools required there is nothing that will do it automatically, that’s why I wrote the following code:

REPORT ZTEST_IDOC_MIGRATION.

* Data declarations
DATA: lt_idoc_list TYPE TABLE OF edidc-docnum,
      lv_idoc_num  TYPE edidc-docnum.


* Selection screen
SELECTION-SCREEN BEGIN OF BLOCK b1. "WITH FRAME TITLE text-001.
PARAMETERS: p_idoc TYPE edidc-docnum.
SELECTION-SCREEN END OF BLOCK b1.

SELECTION-SCREEN BEGIN OF BLOCK b2. "WITH FRAME TITLE text-002.
SELECT-OPTIONS: s_idoc FOR lv_idoc_num.
SELECTION-SCREEN END OF BLOCK b2.

SELECTION-SCREEN BEGIN OF BLOCK b3. "WITH FRAME TITLE text-002.
PARAMETERS: p_dest TYPE string.
SELECTION-SCREEN END OF BLOCK b3.

** Text elements
*TEXT-001 = 'Single IDoc Entry'.
*TEXT-002 = 'Multiple IDoc Entries'.

* Start-of-selection event
START-OF-SELECTION.

* Process single IDoc entry
IF p_idoc IS NOT INITIAL.
  APPEND INITIAL LINE TO lt_idoc_list ASSIGNING FIELD-SYMBOL(<fs_idoc>).
  <fs_idoc> = p_idoc.
ENDIF.

* Process multiple IDoc entries
LOOP AT s_idoc INTO DATA(ls_idoc_range).
  APPEND INITIAL LINE TO lt_idoc_list ASSIGNING FIELD-SYMBOL(<fs_idoc2>).
  <fs_idoc2> = ls_idoc_range-low.
ENDLOOP.

* Further processing of IDocs
LOOP AT lt_idoc_list INTO DATA(ls_idoc).
  " Add your processing logic here
  WRITE: / 'Processing IDoc:', ls_idoc.


DATA: lv_idoc_number TYPE edi_docnum,
      lv_rc          TYPE sy-subrc,
      lt_idoc_data   TYPE TABLE OF edidd,
      lt_idoc_data2  TYPE TABLE OF EDI_DD40,
      lt_idoc_control2 TYPE TABLE OF EDI_DC40,
      ls_idoc_control2 TYPE EDI_DC40,
      ls_idoc_control TYPE edidc.

  REFRESH lt_idoc_data2.
  REFRESH lt_idoc_control2.

*   IDoc number to be sent
*  lv_idoc_number = '0000000000281012'. " Replace with your IDoc number
  lv_idoc_number = ls_idoc.

*   Retrieve IDoc control and data records
  CALL FUNCTION 'IDOC_READ_COMPLETELY'
    EXPORTING
      DOCUMENT_NUMBER = lv_idoc_number
    IMPORTING
      IDOC_CONTROL = ls_idoc_control
    TABLES
      INT_EDIDD = lt_idoc_data
    EXCEPTIONS
      OTHERS = 1.

  IF sy-subrc = 0.
  MOVE-CORRESPONDING lt_idoc_data TO lt_idoc_data2.
  MOVE-CORRESPONDING ls_idoc_control TO ls_idoc_control2.
  ls_idoc_control2-IDOCTYP = ls_idoc_control-IDOCTP.
  APPEND ls_idoc_control2 to lt_idoc_control2.
*   Send IDoc to another SAP system
    CALL FUNCTION 'IDOC_INBOUND_ASYNCHRONOUS' DESTINATION p_dest
      TABLES
        IDOC_CONTROL_REC_40 = lt_idoc_control2
        IDOC_DATA_REC_40 = lt_idoc_data2
      EXCEPTIONS
        OTHERS = 1.

    IF sy-subrc = 0.
      WRITE: 'IDoc sent successfully'.
    ELSE.
      WRITE: 'Error sending IDoc', ls_idoc.
    ENDIF.
  ELSE.
    WRITE: 'Error retrieving IDoc:', ls_idoc.
  ENDIF.
ENDLOOP.

Of course it’s a bit crude and it could be a bit better, for example with a alv report to choose the idoc in style of WE02 etc. and possibly it might need definition of additional data in segments (for example receiver partner), but it worked for my end.

Initial screen consist of the following fields where

In case of success the program will show the following log

And with this quick program migrating idocs, between two connected SAP systems is no longer a problem.

Conclusion

There are standard tools, that can do the job manually, however for some reason SAP doesn’t have a mass transfer one. With this build I will finally forget about manual process.

Leave a Comment

Your email address will not be published. Required fields are marked *