使用する
FUNCTION MASTER_IDOC_CREATE_XAMPLE.*"--------------------------------------------------------------------- *" Lokale Schnittstelle:*" IMPORTING*" VALUE(APPL_HEADER) LIKE XHEAD STRUCTURE XHEAD*" TABLES*" APPL_ITEM STRUCTURE XITEM*"---------------------------------------------------------------------* variables of general interest DATA:* control record for the IDoc IDOC_CONTROL LIKE EDIDC,* data records for the IDoc T_IDOC_DATA LIKE EDIDD OCCURS 0 WITH HEADER LINE,* table for the IDocs created by MASTER_IDOC_CONTROL T_COMM_CONTROL LIKE EDIDC OCCURS 0 WITH HEADER LINE,* partner type for logical system C_PARTNER_TYPE_LOGICAL_SYSTEM LIKE EDIDC-RCVPRT,* help variable for the check if an IDoc has to be created H_CREATE_IDOC.* variables specific for this example DATA:* field strings with IDoc segment structure E1XHEAD LIKE E1XHEAD, E1XITEM LIKE E1XITEM,* data to be put to the control record C_MESSAGE_TYPE LIKE EDIDC-MESTYP VALUE 'XAMPLE', C_BASE_IDOC_TYPE LIKE EDIDC-IDOCTP VALUE 'XAMPLE01',* segment types to be put to the data record table C_HEADER_SEGTYP LIKE EDIDD-SEGNAM VALUE 'E1XHEAD', C_ITEM_SEGTYP LIKE EDIDD-SEGNAM VALUE 'E1XITEM'. * check if an IDoc has to be created, read the distribution modelCALL FUNCTION 'ALE_MODEL_DETERMINE_IF_TO_SEND'EXPORTING MESSAGE_TYPE = C_MESSAGE_TYPE* SENDING_SYSTEM = ' '* RECEIVING_SYSTEM = ' '* VALIDDATE = SY-DATUMIMPORTINGIDOC_MUST_BE_SENT = H_CREATE_IDOC.* exceptions* own_system_not_defined = 1* others = 2.IF H_CREATE_IDOC IS INITIAL.* no message flow maintained in the model, nothing to doEXIT.ENDIF.* put the application header record to the IDocMOVE-CORRESPONDING APPL_HEADER TO E1XHEAD.* convert SAP codes to ISO codesPERFORM E1XHEAD_CODES_SAP_TO_ISO USINGAPPL_HEADER CHANGINGE1XHEAD.* append record to IDoc data tableT_IDOC_DATA-SEGNAM = C_HEADER_SEGTYP.T_IDOC_DATA-SDATA = E1XHEAD.APPEND T_IDOC_DATA.LOOP AT APPL_ITEM.* put the application item record to the IDoc segmentMOVE-CORRESPONDING APPL_ITEM TO E1XITEM.* convert currency amounts from SAP internal to neutral formatPERFORM E1XITEM_CURRENCY_SAP_TO_IDOC USING APPL_HEADER-CURRENCY CHANGINGE1XITEM.* convert SAP codes to ISO codesPERFORM E1XITEM_CODES_SAP_TO_ISO USING APPL_ITEM CHANGINGE1XITEM.* left justify all non character fieldsPERFORM E1XITEM_CONDENSE CHANGINGE1XITEM.* append record to IDoc data table T_IDOC_DATA-SEGNAM = C_ITEM_SEGTYP.T_IDOC_DATA-SDATA = E1XITEM.APPEND T_IDOC_DATA.ENDLOOP.CALL FUNCTION 'MASTER_IDOC_DISTRIBUTE'* in update task "if application document is posted in update taskEXPORTINGMASTER_IDOC_CONTROL = IDOC_CONTROLTABLESCOMMUNICATION_IDOC_CONTROL = T_COMM_CONTROLMASTER_IDOC_DATA = T_IDOC_DATA.* exceptions* error_in_idoc_control = 1 * error_writing_idoc_status = 2 * error_in_idoc_data = 3* sending_logical_system_unknown = 4 * others = 5.* A commit work has to be done. It could also be done in the calling * application.COMMIT WORK.READ TABLE T_COMM_CONTROL INDEX 1.IF SY-SUBRC <> 0.* no IDoc was created, you can react here, if neccessary ENDIF. ENDFUNCTION.*&---------------------------------------------------------------------**& Form E1XITEM_CONDENSE*&---------------------------------------------------------------------** text* *----------------------------------------------------------------------** --> p1 text* <-- p2 text*----------------------------------------------------------------------*FORM E1XITEM_CONDENSECHANGINGIDOC_SEGMENT LIKE E1XITEM.* left justify all non character fieldsCONDENSE: IDOC_SEGMENT-QUANTITY,IDOC_SEGMENT-VALUE.ENDFORM. " E1XITEM_CONDENSE*&---------------------------------------------------------------------**& Form E1XITEM_CURRENCY_SAP_TO_IDOC*&---------------------------------------------------------------------** text **----------------------------------------------------------------------** --> p1 text* <-- p2 text*----------------------------------------------------------------------*FORM E1XITEM_CURRENCY_SAP_TO_IDOCUSING CURRENCY_CODE LIKE TCURC-WAERSCHANGINGIDOC_SEGMENT LIKE E1XITEM.CALL FUNCTION 'CURRENCY_AMOUNT_SAP_TO_IDOC'EXPORTINGCURRENCY = CURRENCY_CODESAP_AMOUNT = IDOC_SEGMENT-VALUEIMPORTINGIDOC_AMOUNT = IDOC_SEGMENT-VALUE.* exceptions* others = 1.ENDFORM. " E1XITEM_CURRENCY_SAP_TO_IDOC*&---------------------------------------------------------------------**& Form E1XHEAD_CODES_SAP_TO_ISO*&---------------------------------------------------------------------** text **----------------------------------------------------------------------** --> p1 text* <-- p2 text*----------------------------------------------------------------------*FORM E1XHEAD_CODES_SAP_TO_ISOUSINGAPPL_DATA LIKE XHEADCHANGINGIDOC_SEGMENT LIKE E1XHEAD.* convert a currency code from SAP code to ISO codeIF NOT APPL_DATA-CURRENCY IS INITIAL.CALL FUNCTION 'CURRENCY_CODE_SAP_TO_ISO'EXPORTINGSAP_CODE = APPL_DATA-CURRENCYIMPORTINGISO_CODE = IDOC_SEGMENT-CURRENCY.* exceptions* not_found = 1* others = 2. ELSE.IDOC_SEGMENT-CURRENCY = APPL_DATA-CURRENCY.ENDIF.* convert a country from SAP code to ISO codeIF NOT APPL_DATA-COUNTRY IS INITIAL.CALL FUNCTION 'COUNTRY_CODE_SAP_TO_ISO'EXPORTINGSAP_CODE = APPL_DATA-COUNTRYIMPORTING ISO_CODE = IDOC_SEGMENT-COUNTRY.* exceptions * not_found = 1* others = 2. ELSE. IDOC_SEGMENT-COUNTRY = APPL_DATA-COUNTRY.ENDIF. ENDFORM. " E1XHEAD_CODES_SAP_TO_ISO*&---------------------------------------------------------------------**& Form E1XITEM_CODES_SAP_TO_ISO *&---------------------------------------------------------------------** text **----------------------------------------------------------------------** --> p1 text* <-- p2 text *----------------------------------------------------------------------*FORM E1XITEM_CODES_SAP_TO_ISOUSINGAPPL_DATA LIKE XITEMCHANGINGIDOC_SEGMENT LIKE E1XITEM.* convert a unit of measure from SAP code to ISO codeIF NOT APPL_DATA-UNIT IS INITIAL.CALL FUNCTION 'UNIT_OF_MEASURE_SAP_TO_ISO'EXPORTINGSAP_CODE = APPL_DATA-UNITIMPORTINGISO_CODE = IDOC_SEGMENT-UNIT.* exceptions* not_found = 1* no_iso_code = 2 * others = 3.ELSE.IDOC_SEGMENT-UNIT = APPL_DATA-UNIT. ENDIF.* convert a package type from SAP code to ISO codeIF NOT APPL_DATA-SHIP_INST IS INITIAL.CALL FUNCTION 'SAP_TO_ISO_PACKAGE_TYPE_CODE'EXPORTINGSAP_CODE = APPL_DATA-SHIP_INSTIMPORTING ISO_CODE = IDOC_SEGMENT-SHIP_INST.* exceptions* not_found = 1 * others = 2.ELSE.IDOC_SEGMENT-SHIP_INST = APPL_DATA-SHIP_INST.ENDIF.ENDFORM. " E1XITEM_CODES_SAP_TO_ISO