Sample Code

Sample program to convert idoc to XML data and some other function module

This is a very simple sample program. First read the control data and then fetch all the EDIDD data using the control record. When we have the entire idoc, then it can be converted to XML.


 DATA: lt_edidd        TYPE STANDARD TABLE OF edidd INITIAL SIZE 0,
        lv_xml_data     TYPE string,
        ls_edidc        TYPE edidc,
        ls_edidc_40     TYPE edi_dc40,
        lt_edidd_40     TYPE STANDARD TABLE OF edi_dd40 INITIAL SIZE 0.

CALL FUNCTION 'EDI_SEGMENTS_GET_ALL'
        EXPORTING
          document_number         = i_edidc-docnum
        TABLES
          idoc_containers         = lt_edidd
        EXCEPTIONS
          document_number_invalid = 1
          end_of_document         = 2
          OTHERS                  = 3.

* Populate the error segment
      LOOP AT lt_edidd INTO DATA(ls_edidd).

        MOVE-CORRESPONDING ls_edidd TO ls_edidd_40.
        APPEND ls_edidd_40 TO lt_edidd_40.
      ENDLOOP.

* POpulate the control record.
      MOVE-CORRESPONDING ls_edidc TO ls_edidc_40.
      ls_edidc_40-idoctyp = i_edidc-idoctp.
      ls_edidc_40-tabnam =  '/'.   "This is important

      CALL FUNCTION 'IDX_IDOC_TO_XML'
        EXPORTING
          edidc40           = ls_edidc_40
          tunnel            = ' '
          rfc_codepage      = lv_codepage
        IMPORTING
          xml_data          = lv_xml_data
        TABLES
          idoc_data_40      = lt_edidd_40
        EXCEPTIONS
          conversion_failed = 1
          system_error      = 2
          structure_error   = 3
          OTHERS            = 4.

XML data comes as a string.

If you want to open and incomplete idoc for processing, you can do that using the FM – EDI_DOCUMENT_OPEN_FOR_PROCESS


    CALL FUNCTION 'EDI_DOCUMENT_OPEN_FOR_PROCESS'
      EXPORTING
        document_number          = i_edidc-docnum
      IMPORTING
        idoc_control             = ls_edidc
      EXCEPTIONS
        document_foreign_lock    = 1
        document_not_exist       = 2
        document_number_invalid  = 3
        document_is_already_open = 4
        OTHERS                   = 5.

Similarly, change the status of the idoc, using the FM -EDI_DOCUMENT_STATUS_SET


      CALL FUNCTION 'EDI_DOCUMENT_STATUS_SET'
        EXPORTING
          document_number         = i_edidc-docnum
          idoc_status             = ls_edids
        EXCEPTIONS
          document_number_invalid = 1
          other_fields_invalid    = 2
          status_invalid          = 3
          OTHERS

Check out the function group – EDI1 for some more useful idoc processing FMs.

As usual, please post comments and feedback and follow us on our social media handles.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)

Follow by Email
Twitter
LinkedIn
LinkedIn
Share