FUNCTION zfm_idoc_return.
*"----------------------------------------------------------------------
"“Local interface:
*” IMPORTING
*" REFERENCE(IV_RCVPRN) TYPE EDI_RCVPRN
*" TABLES
*" IT_RETURN STRUCTURE ZMDME1RETURN
*"----------------------------------------------------------------------
DATA: lt_edidc LIKE edidc OCCURS 0 WITH HEADER LINE,
lt_edidd LIKE edidd OCCURS 0 WITH HEADER LINE.
DATA: lv_logsys LIKE t000-logsys.
DATA: lv_logdes LIKE edipoa-logdes.
CONSTANTS: c_idoc_fill(1) VALUE ‘A’,
c_idoc_save(1) VALUE ‘B’,
c_idoc_send(1) VALUE ‘C’,
c_idoc_process(1) VALUE ‘D’.
CONSTANTS: c_idoc_output_with_nast(1) VALUE ‘1’,
c_idoc_output_change_pointer(1) VALUE ‘2’,
c_idoc_output_without_nast(1) VALUE ‘3’,
c_idoc_outbound_send(1) VALUE ‘4’,
c_idoc_select_and_write_to_db(1) VALUE ‘5’,
c_idoc_process_inbound(1) VALUE ‘6’.
CHECK c_return = ‘X’ .
*------------create a IDoc---------------begin-----------------
REFRESH lt_edidc.
CLEAR lt_edidc.
*****--------------get control data----------begin----------------------
lt_edidc-mandt = sy-mandt.
lt_edidc-docrel = sy-saprl. "SAP release version
lt_edidc-direct = ‘1’.
lt_edidc-rcvprt = ‘LS’.
lt_edidc-rcvprn = iv_rcvprn.
lt_edidc-outmod = ‘2’.
lt_edidc-mestyp = ‘ZMDM_RETURN’. "Messate type
lt_edidc-doctyp = ‘ZMDM_RETURN’. "IDOC type
SELECT SINGLE rcvpor
INTO lt_edidc-rcvpor
FROM edp13
WHERE rcvprn = lt_edidc-rcvprn
AND rcvprt = lt_edidc-rcvprt
AND mestyp = lt_edidc-mestyp.
CHECK sy-subrc = 0 .
lt_edidc-sndprt = ‘LS’.
lt_edidc-serial = sy-datum.
lt_edidc-serial+8 = sy-uzeit.
*- Get Logical System
SELECT SINGLE logsys
INTO lv_logsys
FROM t000
WHERE mandt = sy-mandt.
lt_edidc-sndprn = lv_logsys.
APPEND lt_edidc.
*****--------------get control data----------end----------------------
*****--------------get IDoc data ------------begin--------------------
CLEAR lt_edidd.
REFRESH lt_edidd .
LOOP AT it_return.
CLEAR lt_edidd .
lt_edidd-segnam = ‘ZMDME1RETURN’.
MOVE it_return TO lt_edidd-sdata.
APPEND lt_edidd.
ENDLOOP.
*****--------------get IDoc data ------------end----------------------
CALL FUNCTION ‘IDOC_CCMS_OPEN’
EXPORTING
i_edidc = lt_edidc
idocfunction = c_idoc_save
idocprocess = c_idoc_output_without_nast.
CALL FUNCTION ‘COMMUNICATION_IDOC_CREATE’
TABLES
idoc_control = lt_edidc
idoc_data = lt_edidd
EXCEPTIONS
OTHERS = 04.
CALL FUNCTION ‘IDOC_CCMS_CLOSE’ .
*------------create a IDoc---------------end-------------------
*------------Send IDoc-------------------begin-----------------
*- to avoid lock
CALL FUNCTION ‘EDI_DOCUMENT_DEQUEUE_LATER’
EXPORTING
docnum = lt_edidc-docnum
EXCEPTIONS
idoc_is_not_to_dequeue = 1
OTHERS = 2.
CHECK sy-subrc = 0.
**- open for send
CALL FUNCTION ‘IDOC_CCMS_OPEN’
EXPORTING
i_edidc = lt_edidc
idocfunction = c_idoc_send
idocprocess = c_idoc_outbound_send
PORTTYP = ' '
EXCEPTIONS
insert_error = 1
OTHERS = 2.
CHECK sy-subrc = 0.
CALL FUNCTION ‘IDOC_CCMS_CLOSE’ . " idoc_ccms_close
CALL FUNCTION ‘EDI_OUTPUT_NEW’
EXPORTING
onl_option = ‘B’
error_flag = ’ ’
TABLES
i_edidc = lt_edidc
i_edidd = lt_edidd
EXCEPTIONS
OTHERS = 0.
*------------Send IDoc-------------------end-------------------
*------------Process Error---------------begin-----------------
CALL FUNCTION ‘IDOC_ERROR_WORKFLOW_START’
EXPORTING
docnum = lt_edidc-docnum
eventcode = ‘EDIO’
MESS = ' '
EXCEPTIONSno_entry_in_tede5 = 1
error_in_start_workflow = 2
OTHERS = 0.
*------------Process Error---------------end-------------------
COMMIT WORK.
*if there is no this code, only create a IDoc, but not
*send to the receive system
ENDFUNCTION.