example program to process an idoc - inbound function module
FUNCTION idoc_input_xample. *"---------------------------------------------------------------------- *" *"Local interface: *" IMPORTING *" VALUE(INPUT_METHOD) LIKE BDWFAP_PAR-INPUTMETHD *" VALUE(MASS_PROCESSING) LIKE BDWFAP_PAR-MASS_PROC *" EXPORTING *" VALUE(WORKFLOW_RESULT) LIKE BDWF_PARAM-RESULT *" VALUE(APPLICATION_VARIABLE) LIKE BDWF_PARAM-APPL_VAR *" VALUE(IN_UPDATE_TASK) LIKE BDWFAP_PAR-UPDATETASK *" VALUE(CALL_TRANSACTION_DONE) LIKE BDWFAP_PAR-CALLTRANS *" TABLES *" IDOC_CONTRL STRUCTURE EDIDC *" IDOC_DATA STRUCTURE EDIDD *" IDOC_STATUS STRUCTURE BDIDOCSTAT *" RETURN_VARIABLES STRUCTURE BDWFRETVAR *" SERIALIZATION_INFO STRUCTURE BDI_SER *" EXCEPTIONS *" WRONG_FUNCTION_CALLED *"---------------------------------------------------------------------- * ---------------------------------------------------------------------- * ---------------------- 05 July 1996 ---------------------------------- * ---------------------------------------------------------------------- * Example function module for processing inbound IDocs for ALE or EDI. * This example applies for processing * * with - one IDoc at a time * * without - serialization * - customer-exits * - calling an ALE-enabled transaction * - mass processing (more than one IDoc at a time) * -------------------- Naming conventions ------------------------------ * Internal tables start with 't_' * Internal field strings start with 'f_' * ---------------------------------------------------------------------- * >> The following line must appear in the global part of your * >> function group: * include mbdconwf. "Report containing the ALE constants. * The ALE constants start with 'c_'. DATA: subrc LIKE sy-subrc, object_number LIKE xhead-docmnt_no. * Initialize variables subrc = 0. * Read the IDoc's control record READ TABLE idoc_contrl INDEX 1. * Process the IDoc and post the data to the database PERFORM idoc_process_xample TABLES idoc_data idoc_status USING idoc_contrl CHANGING object_number subrc. * Fill the ALE export parameters CLEAR in_update_task. CLEAR call_transaction_done. "Call Transaction is not used. IF subrc <> 0. "Error occurred workflow_result = c_wf_result_error. return_variables-wf_param = c_wf_par_error_idocs. return_variables-doc_number = idoc_contrl-docnum. APPEND return_variables. ELSE. "IDoc processed successfully workflow_result = c_wf_result_ok. return_variables-wf_param = c_wf_par_processed_idocs. return_variables-doc_number = idoc_contrl-docnum. APPEND return_variables. return_variables-wf_param = c_wf_par_appl_objects. return_variables-doc_number = object_number. APPEND return_variables. ELSE. ENDFUNCTION. *---------------------------------------------------------------------* * FORM IDOC_PROCESS_XAMPLE * *---------------------------------------------------------------------* * This routine creates an application document based on the IDoc's * * contents. Object_Number contains the new document's number. * * If an error occurs, subrc is non-zero, t_idoc_status is filled. * * Note: if more than one error is detected, t_idoc_status contains * * more than one status record. * *---------------------------------------------------------------------* * --> F_IDOC_CONTRL IDoc control record * * --> T_IDOC_DATA IDoc data records * * <-- T_IDOC_STATUS IDoc status records * * <-- OBJECT_NUMBER Created document's number * * <-- SUBRC Return code * *---------------------------------------------------------------------* FORM idoc_process_xample TABLES t_idoc_data STRUCTURE edidd t_idoc_status STRUCTURE bdidocstat USING f_idoc_contrl STRUCTURE edidc CHANGING object_number LIKE xhead-docmnt_no subrc LIKE sy-subrc. * Internal field string for the document header. DATA: f_xhead LIKE xhead. * Internal table for the document items. DATA: t_xitem LIKE xitem OCCURS 0 WITH HEADER LINE. * Number given to the created document DOCUMENT_NUMBER LIKE f_xhead-docmnt_no. * Move the data in the IDoc to the internal structures/tables * f_xhead and t_xitem. PERFORM idoc_interpret TABLES t_idoc_data t_xitem t_idoc_status USING f_idoc_contrl CHANGING f_xhead subrc. * Create the application object if no error occurred so far. IF subrc = 0. * This fictitious function module creates a new object based on the * data that was read from the IDoc. The new object's ID is returned * in the parameter 'document_number'. * The function module checks that the data is correct, and raises * an exception if an error is detected. CALL FUNCTION 'XAMPLE_OBJECT_CREATE' EXPORTING xhead = f_xhead IMPORTING document_number = document_number TABLES xitem = t_xitem EXCEPTIONS OTHERS = 1. IF sy-subrc <> 0. subrc = 1. * Put the error message into 't_idoc_status' PERFORM status_fill_sy_error TABLES t_idoc_status USING t_idoc_data sy '' "Field name 'idoc_process_xample'. "Form routine ELSE. * Fill the remaining export parameters object_number = document_number. "New document's number t_idoc_status-docnum = f_idoc_contrl-docnum. t_idoc_status-status = c_idoc_status_ok. t_idoc_status-msgty = 'S'. t_idoc_status-msgid = your_msgid. "Global variable. t_idoc_status-msgno = msgno_success."Global variable. t_idoc_status-msgv1 = object_number. APPEND t_idoc_status. ENDIF. "if sy-subrc <> 0. ENDIF. "if subrc = 0. ENDFORM. "IDOC_PROCESS_XAMPLE *---------------------------------------------------------------------* * FORM IDOC_INTERPRET * *---------------------------------------------------------------------* * This routine checks that the correct message type is being used, * * and then converts and moves the data from the IDoc segments to the * * internal structure f_xhead and internal table t_xitem. * * If an error occurs, t_idoc_status is filled an subrc <> 0. * *---------------------------------------------------------------------* * --> T_IDOC_STATUS * * --> T_XITEM * * --> F_IDOC_DATA * * --> F_XHEAD * * --> SUBRC * *---------------------------------------------------------------------* FORM idoc_interpret TABLES t_idoc_data STRUCTURE edidd t_xitem STRUCTURE xitem t_idoc_status STRUCTURE bdidocstat USING f_idoc_contrl STRUCTURE edidc CHANGING f_xhead STRUCTURE xhead subrc LIKE sy-subrc. * Check that the IDoc contains the correct message type. * Note: if your message type is reducible, check field 'idoctp' * (IDoc type) instead of 'mestyp'. IF f_idoc_contrl-mestyp <> 'XAMPLE'. MESSAGE ID your_msgid "Global variable TYPE 'E' NUMBER msgno_wrong_function "Global variable WITH f_idoc_contrl-mestyp "message type 'IDOC_INPUT_XAMPLE' "Your function module. f_idoc_contrl-sndprt "Sender partner type f_idoc_contrl-sndprn "Sender number. RAISING wrong_function_called. ENDIF. * Loop through the IDoc's segments and convert the data from the IDoc * format to the internal format. LOOP AT t_idoc_data WHERE docnum = f_idoc_contrl-docnum. CASE t_idoc_data-segnam. WHEN 'E1XHEAD'. PERFORM e1xhead_process TABLES t_idoc_status USING t_idoc_data CHANGING f_xhead subrc. WHEN 'E1XITEM'. PERFORM e1xitem_process TABLES t_xitem t_idoc_status USING f_xhead-currency t_idoc_data CHANGING subrc. ENDCASE. ENDLOOP. ENDFORM. "IDOC_INTERPRET *---------------------------------------------------------------------* * FORM E1XHEAD_PROCESS * *---------------------------------------------------------------------* * This routine fills 'f_xhead' out of segment e1xhead. * * If an error occurs, subrc is non-zero, t_idoc_status is filled. * *---------------------------------------------------------------------* * --> F_IDOC_DATA IDoc segment containing e1xhead fields * * <-- F_XHEAD Internal structure containing doc. header * * <-- T_IDOC_STATUS Status fields for error handling * * <-- SUBRC Return code: non-zero if an error occurred * *---------------------------------------------------------------------* FORM e1xhead_process TABLES t_idoc_status STRUCTURE bdidocstat USING f_idoc_data STRUCTURE edidd CHANGING f_xhead STRUCTURE xhead subrc LIKE sy-subrc. DATA: f_e1xhead LIKE e1xhead. f_e1xhead = f_idoc_data-sdata. * Process fields that need conversion from ISO-codes to SAP-codes PERFORM e1xhead_codes_iso_to_sap TABLES t_idoc_status USING f_e1xhead f_idoc_data CHANGING f_xhead subrc. * Process fields containing dates or times PERFORM e1xhead_date_time USING f_e1xhead CHANGING f_xhead. ENDFORM. "e1xhead_process *---------------------------------------------------------------------* * FORM E1XITEM_PROCESS * *---------------------------------------------------------------------* * This routine converts the data in the segment 'e1xitem' for * * to the format of table 't_xitem' and appends it to the table. * * If an error occurs, subrc is non-zero, t_idoc_status is filled. * *---------------------------------------------------------------------* * --> F_IDOC_DATA IDoc segment * * <-- T_XITEM Document items to be updated to database * * <-- T_IDOC_STATUS Status fields filled if an error occurred * * <-- SUBRC Return code: 0 if all OK * *---------------------------------------------------------------------* FORM e1xitem_process TABLES t_xitem STRUCTURE xitem t_idoc_status STRUCTURE bdidocstat USING currency LIKE xhead-currency f_idoc_data STRUCTURE edidd CHANGING subrc LIKE sy-subrc. DATA: f_e1xitem LIKE e1xitem. f_e1xitem = f_idoc_data-sdata. * Fields of type CHAR, NUMC, QUAN need no conversion. t_xitem-item_no = f_e1xitem-item_no. t_xitem-materialid = f_e1xitem-materialid. t_xitem-descript = f_e1xitem-descript. t_xitem-quantity = f_e1xitem-quantity. * Process fields that need conversion from ISO-codes to SAP-codes PERFORM e1xhead_codes_iso_to_sap TABLES t_idoc_status USING f_e1xhead f_idoc_data CHANGING f_xhead subrc. * Process fields that contain monetary values PERFORM e1xitem_value_idoc_to_sap TABLES t_idoc_status USING f_e1xitem currency f_idoc_data CHANGING t_xitem subrc. APPEND t_xitem. ENDFORM. "E1XITEM_PROCESS *---------------------------------------------------------------------* * FORM E1XHEAD_CODES_ISO_TO_SAP * *---------------------------------------------------------------------* * Converts ISO-Codes in f_e1xhead to SAP-codes in f_xhead. * * f_idoc_data, t_idoc_status and subrc are used for error handling. * *---------------------------------------------------------------------* FORM e1xhead_codes_iso_to_sap TABLES t_idoc_status STRUCTURE bdidocstat USING f_e1xhead STRUCTURE e1xhead f_idoc_data STRUCTURE edidd CHANGING f_xhead STRUCTURE xhead subrc. * f_xhead-currency Type CUKY => convert ISO-Code to SAP-Code. PERFORM currency_code_iso_to_sap TABLES t_idoc_status USING f_e1xhead-currency f_idoc_data 'CURRENCY' CHANGING f_xhead-currency subrc. CHECK subrc = 0. * f_xhead-country Contains a country => convert from ISO to SAP code. PERFORM country_code_iso_to_sap TABLES t_idoc_status USING f_e1xhead-country f_idoc_data 'COUNTRY' CHANGING f_xhead-country subrc. ENDFORM. "E1XHEAD_CODES_ISO_TO_SAP *---------------------------------------------------------------------* * FORM E1XITEM_CODES_ISO_TO_SAP * *---------------------------------------------------------------------* * Converts ISO-Codes in f_e1xitem to SAP-codes in f_xitem * * f_idoc_data, t_idoc_status and subrc are used for error handling. * *---------------------------------------------------------------------* FORM e1xitem_codes_iso_to_sap TABLES t_idoc_status STRUCTURE bdidocstat USING f_e1xitem STRUCTURE e1xitem f_idoc_data STRUCTURE edidd CHANGING f_xitem STRUCTURE xitem subrc LIKE sy-subrc. * f_xitem-unit Type UNIT => convert ISO-Code to SAP-Code. PERFORM unit_of_measure_iso_to_sap TABLES t_idoc_status USING f_e1xitem-unit f_idoc_data 'unit' CHANGING f_xitem-unit subrc. * f_xitem-ship_inst Contains shipping instructions => ISO to SAP code. PERFORM shipping_instruct_iso_to_sap TABLES t_idoc_status USING f_e1xitem-ship_inst f_idoc_data 'ship_inst' CHANGING f_xitem-ship_inst subrc. ENDFORM. "E1XITEM_CODES_ISO_TO_SAP *---------------------------------------------------------------------* * FORM E1XITEM_VALUE_IDOC_TO_SAP * *---------------------------------------------------------------------* * Converts fields containing monetary values in f_e1xitem to * * the internal representation in f_xitem. * * f_idoc_data, t_idoc_status and subrc are used for error handling. * *---------------------------------------------------------------------* FORM e1xitem_value_idoc_to_sap TABLES t_idoc_status STRUCTURE bdidocstat USING f_e1xitem STRUCTURE e1xitem currency LIKE xhead-currency f_idoc_data STRUCTURE edidd CHANGING f_xitem STRUCTURE xitem subrc LIKE sy-subrc. * f_xitem-value Type CURR => convert IDoc amount to internal amount. * N.B. the currency code used here must be the SAP-internal one, not * the one contained in the IDoc! CALL FUNCTION 'CURRENCY_AMOUNT_IDOC_TO_SAP' EXPORTING currency = currency idoc_amount = f_e1xitem-value IMPORTING sap_amount = f_xitem-value EXCEPTIONS OTHERS = 1. IF sy-subrc <> 0. subrc = 1. * Put the error message into 't_idoc_status' PERFORM status_fill_sy_error TABLES t_idoc_status USING f_idoc_data sy 'value' "Field name 'e1xitem_value_idoc_to_sap'. "Form routine ENDIF. "if sy-subrc <> 0. ENDFORM. "E1XITEM_VALUE_IDOC_TO_SAP *---------------------------------------------------------------------* * FORM E1XHEAD_DATE_TIME * *---------------------------------------------------------------------* * Moves date and time fields in f_e1xhead to the fields in f_xhead. * *---------------------------------------------------------------------* FORM e1xhead_date_time USING f_e1xhead STRUCTURE e1xhead CHANGING f_xhead STRUCTURE xhead. * f_xhead-date Type DATS => initial value is not 'blank'. IF e1xhead-date IS INITIAL. CLEAR f_xhead-date. f_xhead-date = f_e1xhead-date. ENDIF. ENDFORM. "E1XHEAD_DATE_TIME *---------------------------------------------------------------------* * FORM CURRENCY_CODE_ISO_TO_SAP * *---------------------------------------------------------------------* * Converts ISO currency code 'iso_currency_code' to SAP code in * * 'sap_currency_code' * * f_idoc_data, field_name, t_idoc_status and subrc are used for * * for error handling. * *---------------------------------------------------------------------* FORM currency_code_iso_to_sap TABLES t_idoc_status STRUCTURE bdidocstat USING iso_currency_code LIKE tcurc-isocd f_idoc_data STRUCTURE edidd field_name LIKE bdidocstat-segfld CHANGING sap_currency_code LIKE tcurc-waers subrc LIKE sy-subrc. IF iso_currency_code IS INITIAL. CLEAR sap_currency_code. ELSE. CALL FUNCTION 'CURRENCY_CODE_ISO_TO_SAP' EXPORTING iso_code = iso_currency_code IMPORTING sap_code = sap_currency_code EXCEPTIONS OTHERS = 1. IF sy-subrc <> 0. subrc = 1. * Put the error message into 't_idoc_status' PERFORM status_fill_sy_error TABLES t_idoc_status USING f_idoc_data sy field_name 'currency_code_iso_to_sap'. "Form routine ENDIF. "if sy-subrc <> 0. ENDIF. "if iso_currency_code is initial. ENDFORM. "CURRENCY_CODE_ISO_TO_SAP *---------------------------------------------------------------------* * FORM CURRENCY_CODE_ISO_TO_SAP * *---------------------------------------------------------------------* * Converts ISO currency code 'iso_currency_code' to SAP code in * * 'sap_currency_code' * * f_idoc_data, field_name, t_idoc_status and subrc are used for * * for error handling. * *---------------------------------------------------------------------* FORM country_code_iso_to_sap TABLES t_idoc_status STRUCTURE bdidocstat USING iso_country_code LIKE t005-intca f_idoc_data STRUCTURE edidd field_name LIKE bdidocstat-segfld CHANGING sap_country_code LIKE t005-land1 subrc LIKE sy-subrc. * Only convert if the field is not initial. IF iso_country_code IS INITIAL. CLEAR sap_country_code. ELSE. CALL FUNCTION 'COUNTRY_CODE_ISO_TO_SAP' EXPORTING iso_code = iso_country_code IMPORTING sap_code = sap_country_code EXCEPTIONS OTHERS = 1. IF sy-subrc <> 0. subrc = 1. * Put the error message into 't_idoc_status' PERFORM status_fill_sy_error TABLES t_idoc_status USING f_idoc_data sy field_name 'country_code_iso_to_sap'. "Form routine ENDIF. "if sy-subrc <> 0. ENDIF. "if iso_country_code is initial. ENDFORM. "COUNTRY_CODE_ISO_TO_SAP *---------------------------------------------------------------------* * FORM UNIT_OF_MEASURE_ISO_TO_SAP * *---------------------------------------------------------------------* * Converts ISO unit of measure code 'iso_unit_of_measure' to SAP * * code in 'sap_unit_of_measure'. * * f_idoc_data, field_name, t_idoc_status and subrc are used for * * for error handling. * *---------------------------------------------------------------------* FORM unit_of_measure_iso_to_sap TABLES t_idoc_status STRUCTURE bdidocstat USING iso_unit_of_measure LIKE t006-isocode f_idoc_data STRUCTURE edidd field_name LIKE bdidocstat-segfld CHANGING sap_unit_of_measure LIKE t006-msehi subrc LIKE sy-subrc. * Only convert the field if it is not empty. IF iso_unit_of_measure IS INITIAL. CLEAR sap_unit_of_measure. ELSE. CALL FUNCTION 'UNIT_OF_MEASURE_ISO_TO_SAP' EXPORTING iso_code = iso_unit_of_measure IMPORTING sap_code = sap_unit_of_measure EXCEPTIONS OTHERS = 1. IF sy-subrc <> 0. subrc = 1. * Put the error message into 't_idoc_status' PERFORM status_fill_sy_error TABLES t_idoc_status USING f_idoc_data sy field_name 'unit_of_measure_iso_to_sap'. "Form routine ENDIF. "if sy-subrc <> 0. ENDIF. "if iso_unit_of_measure_code is initial. ENDFORM. "UNIT_OF_MEASURE_ISO_TO_SAP *---------------------------------------------------------------------* * FORM SHIPPING_INSTRUCT_ISO_TO_SAP * *---------------------------------------------------------------------* * Converts ISO package code 'iso_package_type' to SAP code for * * purchasing shipping instructions in 'sap_shipping_instructions'. * * f_idoc_data, field_name, t_idoc_status and subrc are used for * * for error handling. * *---------------------------------------------------------------------* FORM shipping_instruct_iso_to_sap TABLES t_idoc_status STRUCTURE bdidocstat USING iso_package_type LIKE t027a-ivers f_idoc_data STRUCTURE edidd field_name LIKE bdidocstat-segfld CHANGING sap_shipping_instructions LIKE t027a-evers subrc LIKE sy-subrc. * Only convert the field if it is not empty. IF iso_package_type IS INITIAL. CLEAR sap_shipping_instructions. ELSE. CALL FUNCTION 'ISO_TO_SAP_PACKAGE_TYPE_CODE' EXPORTING iso_code = iso_package_type IMPORTING sap_code = sap_shipping_instructions EXCEPTIONS OTHERS = 1. IF sy-subrc <> 0. subrc = 1. * Put the error message into 't_idoc_status' PERFORM status_fill_sy_error TABLES t_idoc_status USING f_idoc_data sy field_name 'shipping_instruct_iso_to_sap'. "Form rout. ENDIF. "if sy-subrc <> 0. ENDIF. "if iso_unit_of_measure_code is initial. ENDFORM. "SHIPPING_INSTRUCT_ISO_TO_SAP *---------------------------------------------------------------------* * FORM STATUS_FILL_SY_ERROR * *---------------------------------------------------------------------* * Fills the structure t_idoc_status with the import parameters * * plus the relevant sy fields. * *---------------------------------------------------------------------* * --> IDOC_NUMBER IDoc number * * --> SEGNUM Segment number * * --> SEGFLD Field in segment * * --> ROUTID Name of routine * * <-- T_IDOC_STATUS Status fields * *---------------------------------------------------------------------* FORM status_fill_sy_error TABLES t_idoc_status STRUCTURE bdidocstat USING f_idoc_data STRUCTURE edidd value(f_sy) STRUCTURE sy segfld LIKE bdidocstat-segfld routid LIKE bdidocstat-routid. t_idoc_status-docnum = f_idoc_data-docnum. t_idoc_status-status = c_idoc_status_error. t_idoc_status-msgty = f_sy-msgty. t_idoc_status-msgid = f_sy-msgid. t_idoc_status-msgno = f_sy-msgno. t_idoc_status-msgv1 = f_sy-msgv1. t_idoc_status-msgv2 = f_sy-msgv2. t_idoc_status-msgv3 = f_sy-msgv3. t_idoc_status-msgv4 = f_sy-msgv4. t_idoc_status-segnum = f_idoc_data-segnum. t_idoc_status-segfld = segfld. t_idoc_status-repid = f_sy-repid. t_idoc_status-routid = routid. APPEND t_idoc_status. ENDFORM. "STATUS_FILL_SY_ERROR