Aug 12, 2012

Creating a Function Module (Direct Inbound Processing)

Use
This step describes how to create a function module which is identified by the IDoc Interface using a new process code and called from ALE (field TBD52-FUNCNAME). Direct inbound processing using a function module (not using a workflow) always includes the ALE layer. This setting (processing with function module and ALE layer) is identified by the value 6 in field TEDE2-EDIVRS, which is read by the function module IDOC_START_INBOUND. IDOC_START_INBOUND then calls ALE.

Prerequisites
You must have completed the required steps in Defining and Using a Basic Type.

Process
  1. Choose Start of the navigation path Tools Next navigation step ABAP Workbench  Next navigation step Development Next navigation step Function Builder End of the navigation path and create a new function module.
  2. Create the segments as global data in your function group. The function module should copy the application data from the segments into the corresponding application tables and modify the IDoc status accordingly. If an error occurs, the function module must set the corresponding workflow parameters for exception handling.
  3. Activate the function module. On the initial Function Builder screen, choose Activate.


Example
In the example, create function module IDOC_INPUT_TESTER with a global interface. The function module is called when an IDoc of type TESTER01 is received for inbound processing. You will assign an application object standard order to this IDoc type and therefore maintain tables from SD. To do this, call transaction VA01 using the command CALL TRANSACTION. Please note that the intention here is not to simulate a realistic standard order, but only to illustrate how data reaches application tables from an IDoc table via segment structures (form routine READ_IDOC_TESTER) and how the function module triggers an event for exception handling (by returning suitable return variables to the ALE layer in the FORM routine RETURN_VARIABLES_FILL).

Note
You can find a comprehensive example for the source code of an inbound function module in the ALE documentation of the SAP library under Example Program for Creating IDocs For example, this function module also checks the correct logical message and calls (fictitious) second function module that first writes the application data and then returns the number of the resulting document. In addition, status 53 is only set if the application document was posted correctly.

Administration parameters for IDOC_INPUT_TESTER
Application abbreviationV (Sales and Distribution)
Processing typeNormal, start immediately

Interface for IDOC_INPUT_TESTER (global interface)
Formal parametersReference structureExplanation
Import parameters
INPUT_METHODBDWFAP_PAR-INPUTMETHDDescribes how the function module is to be processed (example: in the background)
MASS_PROCESSINGBDWFAP_PAR-MASS_PROCMass inbound processing? (indicator)
Export parameters
WORKFLOW_RESULTBDWFAP_PAR-RESULTSet to 99999 if an event is to be triggered for error handling.
APPLICATION_VARIABLEBDWFAP_PAR-APPL_VARVariable freely available from application for workflow
IN_UPDATE_TASKBDWFAP_PAR-UPDATETASKAsynchronous update? (indicator is not set in example)
CALL_TRANSACTION_DONEBDWFAP_PAR-CALLTRANSTransaction called? (indicator is not set in example)
Table
IDOC_CONTRLEDIDCIDoc control record
IDOC_DATAEDIDDIDoc data records
IDOC_STATUSBDIDOCSTATIDoc status records for ALE
RETURN_VARIABLESBDWFRETVARIDoc assigned to Object type method parameters.
SERIALIZATION_INFOBDI_SERIf several IDocs are to be processed in a certain sequence: this structure contains the necessary information


Example

FUNCTION IDOC_INPUT_TESTER.
*"-------------------------------------------------------------------
*"*"Globale Schnittstelle:
*"       IMPORTING
*"             VALUE(INPUT_METHOD) LIKE  BDWFAP_PAR-INPUTMETHD
*"             VALUE(MASS_PROCESSING) LIKE  BDWFAP_PAR-MASS_PROC
*"       EXPORTING
*"             VALUE(WORKFLOW_RESULT) LIKE  BDWFAP_PAR-RESULT
*"             VALUE(APPLICATION_VARIABLE) LIKE  BDWFAP_PAR-APPL_VAR
*"             VALUE(IN_UPDATE_TASK) LIKE  BDWFAP_PAR-UPDATETASK
*"             VALUE(CALL_TRANSACTION_DONE) LIKE  BDWFAP_PAR-CALLTRANS
*"       TABLES
*"              IDOC_CONTRL STRUCTURE  EDIDC OPTIONAL
*"              IDOC_DATA STRUCTURE  EDIDD
*"              IDOC_STATUS STRUCTURE  BDIDOCSTAT
*"              RETURN_VARIABLES STRUCTURE  BDWFRETVAR
*"              SERIALIZATION_INFO STRUCTURE  BDI_SER
*"-------------------------------------------------------------------

* initialize SET/GET Parameter and internal tables
  PERFORM INITIALIZE_ORGANIZATIONAL_DATA.
* Move IDOC to internal tables of application
  PERFORM READ_IDOC_TESTER.
* call transaction Order Entry VA01
  PERFORM CALL_VA01_IDOC_ORDERS USING ERRORCODE.
* set status value
  perform write_status_record using errorcode.
* return values of function module
  PERFORM RETURN_VARIABLES_FILL USING ERRORCODE.

ENDFUNCTION.


FORM INITIALIZE_ORGANIZATIONAL_DATA.

* initialize SET/GET parameters
   SET PARAMETER ID 'VKO' FIELD SPACE.
   SET PARAMETER ID 'VTW' FIELD SPACE.
   SET PARAMETER ID 'SPA' FIELD SPACE.
   SET PARAMETER ID 'VKB' FIELD SPACE.
   SET PARAMETER ID 'VKG' FIELD SPACE.

* initialize internal tables
   REFRESH BDCDATA.
   CLEAR BDCDATA.
   CLEAR BELEGNUMMER.
   CLEAR ERRTAB.
   REFRESH ERRTAB.
   REFRESH XBDCMSGCOLL.
   CLEAR XBDCMSGCOLL.

ENDFORM.                    " INITIALIZE_ORGANIZATIONAL_DATA

FORM READ_IDOC_TESTER.

  PERFORM INITIALIZE_IDOC.
 LOOP AT IDOC_DATA
   WHERE DOCNUM = IDOC_CONTRL-DOCNUM.
    CASE IDOC_DATA-SEGNAM.
* header data
      WHEN 'E1HEAD'.
        MOVE IDOC_DATA-SDATA TO E1HEAD.
        PERFORM PROCESS_SEGMENT_E1HEAD.
* position data
      WHEN 'E1ITEM'.
        MOVE IDOC_DATA-SDATA TO E1ITEM.
        PERFORM PROCESS_SEGMENT_E1ITEM.
    ENDCASE.
 ENDLOOP.
* only when there were one or more items
  CHECK FIRST NE 'X'.
  APPEND XVBAP.                        "last one

ENDFORM.                    " READ_IDOC_TESTER

FORM INITIALIZE_IDOC.

  CLEAR XVBAK.
  REFRESH XVBAP.
  CLEAR XVBAP.
  POSNR = 0.
  FIRST = 'X'.

ENDFORM.                    " INITIALIZE_IDOC

FORM PROCESS_SEGMENT_E1HEAD.

* requested date of delivery
  WLDAT = E1HEAD-WLDAT.
* delivery date
  XVBAK-BSTDK = E1HEAD-BSTDK.
* customer number
  XVBAK-KUNNR = E1HEAD-AUGEB.
* order number
  XVBAK-BSTNK = E1HEAD-BELNR.
* division
  XVBAK-SPART = E1HEAD-SPART.
* distribution channel
  XVBAK-VTWEG = E1HEAD-VTWEG.
* sales organization
  XVBAK-VKORG = E1HEAD-VKORG.
* order type
  XVBAK-AUART = E1HEAD-AUART.
* do not fill incoterms (inco1, inco2)
* customer function
  CALL CUSTOMER-FUNCTION '001'
       EXPORTING
            PI_VBAK621           = XVBAK
       IMPORTING
            PE_VBAK621           = XVBAK
       TABLES
            PT_IDOC_DATA_RECORDS = IDOC_DATA.

ENDFORM.                    " PROCESS_SEGMENT_E1HEAD

FORM PROCESS_SEGMENT_E1ITEM.
* position number
  XVBAP-POSNR = XVBAP-POSNR + 1.
* amount
  XVBAP-WMENG = E1ITEM-MENGE.
* unit
  CALL FUNCTION 'ISO_TO_SAP_MEASURE_UNIT_CODE'
       EXPORTING
            ISO_CODE  = E1ITEM-BMEINH
       IMPORTING
            SAP_CODE  = XVBAP-VRKME
       EXCEPTIONS
            OTHERS    = 0.
* material number
  XVBAP-MATNR = E1ITEM-LMATNR.

CALL CUSTOMER-FUNCTION '002'
       EXPORTING
            PI_VBAP621           = XVBAP
       IMPORTING
            PE_VBAP621           = XVBAP
       TABLES
            PT_IDOC_DATA_RECORDS = IDOC_DATA.
APPEND XVBAP.

ENDFORM.                    " PROCESS_SEGMENT_E1ITEM

FORM CALL_VA01_IDOC_ORDERS USING ERRORCODE.

* call transaction first dynpro
  PERFORM DYNPRO_START.
* call transaction double-line entry
  PERFORM DYNPRO_DETAIL2.
* incoterms
  PERFORM DYNPRO_HEAD_300.
* call transaction item datas
  PERFORM DYNPRO_POSITION.
  PERFORM DYNPRO_SET USING 'BDC_OKCODE' 'SICH'.
* determine input method
  IF INPUT_METHOD IS INITIAL.
    INPUT_METHOD = 'N'.
  ENDIF.
* call transaction VA01
 CALL TRANSACTION 'VA01' USING    BDCDATA
                         MODE     INPUT_METHOD
                         UPDATE   'S'
                         MESSAGES INTO XBDCMSGCOLL.


errorcode = SY-SUBRC.       " remember returncode for status update

ENDFORM.                    " CALL_VA01_IDOC_ORDERS


form write_status_record using errorcode.

* FILL IDOC_STATUS
 IDOC_STATUS-DOCNUM = IDOC_CONTRL-DOCNUM.
 IF ERRORCODE = 0.
IDOC_STATUS-STATUS = BELEG_GEBUCHT. "value 53
   GET PARAMETER ID 'AUN' FIELD BELEGNUMMER.
   IDOC_STATUS-MSGID = 'V1'.
   IDOC_STATUS-MSGNO = '311'.
   IDOC_STATUS-MSGV1 = 'Terminauftrag'.
   IDOC_STATUS-MSGV2 = BELEGNUMMER.
 ELSE.
    IDOC_STATUS-STATUS = BELEG_NICHT_GEBUCHT. "value 51
    IDOC_STATUS-MSGID = SY-MSwGID.
    IDOC_STATUS-MSGNO = SY-MSGNO.
    IDOC_STATUS-MSGV1 = SY-MSGV1.
    IDOC_STATUS-MSGV2 = SY-MSGV2.
    IDOC_STATUS-MSGV3 = SY-MSGV3.
    IDOC_STATUS-MSGV4 = SY-MSGV4.
  ENDIF.
  APPEND IDOC_STATUS.

ENDFORM.


FORM DYNPRO_START.

  PERFORM DYNPRO_NEW USING PROGRAMM_AUFTRAG
                           DYNPRO-EINSTIEG
                  CHANGING LAST_DYNPRO.
* ordertype
  PERFORM DYNPRO_SET USING 'VBAK-AUART' XVBAK-AUART.
* sales organization
  PERFORM DYNPRO_SET USING 'VBAK-VKORG' XVBAK-VKORG.
* Distribution channel
  PERFORM DYNPRO_SET USING 'VBAK-VTWEG' XVBAK-VTWEG.
* Division
  PERFORM DYNPRO_SET USING 'VBAK-SPART' XVBAK-SPART.
* Sales office
  PERFORM DYNPRO_SET USING 'VBAK-VKBUR' XVBAK-VKBUR.
* Sales group
  PERFORM DYNPRO_SET USING 'VBAK-VKGRP' XVBAK-VKGRP.

ENDFORM.                    " DYNPRO_START


FORM DYNPRO_NEW USING    PROGNAME
                         DYNPRONR
                CHANGING LAST_DYNPRO.

 CLEAR BDCDATA.
 BDCDATA-PROGRAM = PROGNAME.
 BDCDATA-DYNPRO  = DYNPRONR.
 BDCDATA-DYNBEGIN   = 'X'.
 APPEND BDCDATA.
 LAST_DYNPRO = DYNPRONR.

ENDFORM.                    " DYNPRO_NEW


FORM DYNPRO_SET USING    FELDNAME
                         FELDINHALT.

  CLEAR BDCDATA.
  CHECK FELDINHALT NE SPACE.
* dynpro field name
  BDCDATA-FNAM = FELDNAME.
* contents
  BDCDATA-FVAL = FELDINHALT.
  APPEND  BDCDATA.

ENDFORM.                    " DYNPRO_SET

FORM DYNPRO_DETAIL2.
* okcode
*  PERFORM DYNPRO_SET USING 'BDC_OKCODE' PANEL-UER2.
* fix dynpro number 4001
  PERFORM DYNPRO_NEW  USING    PROGRAMM_AUFTRAG
                               '4001'
                      CHANGING LAST_DYNPRO.
* order party
  PERFORM DYNPRO_SET      USING 'KUAGV-KUNNR'  XVBAK-KUNNR.
* purchase order number
  PERFORM DYNPRO_SET      USING 'VBKD-BSTKD'   XVBAK-BSTNK.
* requested delivery date
  PERFORM DYNPRO_DATE_SET USING 'VBKD-BSTDK'   XVBAK-BSTDK.
* purchase order date
  PERFORM DYNPRO_DATE_SET USING 'RV45A-KETDAT' WLDAT.

ENDFORM.                    " DYNPRO_DETAIL2

FORM DYNPRO_DATE_SET USING    FELDNAME
                              FELDINHALT.

  DATA: DATE TYPE D.

  CLEAR BDCDATA.
  CHECK FELDINHALT NE SPACE.
  BDCDATA-FNAM = FELDNAME.
  WRITE FELDINHALT  TO DATE.
  BDCDATA-FVAL = DATE.
  APPEND  BDCDATA.

ENDFORM.                    " DYNPRO_DATE_SET

FORM DYNPRO_HEAD_300.

  PERFORM DYNPRO_SET USING 'BDC_OKCODE' PANEL-KKAU.

* incoterms part 1
  IF NOT XVBAK-INCO1 IS INITIAL.
   PERFORM DYNPRO_SET USING 'VBKD-INCO1' XVBAK-INCO1.
  ENDIF.
* incoterms part 2
  IF NOT XVBAK-INCO2 IS INITIAL.
   PERFORM DYNPRO_SET USING 'VBKD-INCO2' XVBAK-INCO2.
  ENDIF.
*  PERFORM DYNPRO_SET USING 'BDC_OKCODE' 'BACK'.

ENDFORM.                    " DYNPRO_HEAD_300

FORM DYNPRO_POSITION.

  LOOP AT XVBAP.
* dynpro item double line entry
*   PERFORM DYNPRO_SET USING 'BDC_OKCODE' 'UER2'.

    IF XVBAP-POSNR = 1.
* material number
      PERFORM DYNPRO_SET      USING 'VBAP-MATNR(01)'   XVBAP-MATNR.
* order quantity
      PERFORM DYNPRO_SET      USING 'RV45A-KWMENG(01)' XVBAP-WMENG.
* desired delivery date
      PERFORM DYNPRO_DATE_SET USING 'RV45A-ETDAT(1)'  WLDAT.
* sales unit
      PERFORM DYNPRO_SET      USING 'VBAP-VRKME(1)'   XVBAP-VRKME.
    ELSE.
*      PERFORM DYNPRO_SET      USING 'BDC_OKCODE'      'POAN'.

* material number
      PERFORM DYNPRO_SET      USING 'VBAP-MATNR(02)'    XVBAP-MATNR.
* order quantity
      PERFORM DYNPRO_SET      USING 'RV45A-KWMENG(02)'  XVBAP-WMENG.
* desired delivery date
      PERFORM DYNPRO_DATE_SET USING 'RV45A-ETDAT(02)'   WLDAT.
* sales unit
      PERFORM DYNPRO_SET      USING 'VBAP-VRKME(02)'    XVBAP-VRKME.
    ENDIF.
  ENDLOOP.

ENDFORM.                    " DYNPRO_POSITION

FORM RETURN_VARIABLES_FILL USING ERRORCODE.

* allocate IDOC numbers to Workflow output parameters
  IF MASS_PROCESSING <> SPACE.
    IF ERRORCODE = 0.

      RETURN_VARIABLES-WF_PARAM = PID.
      RETURN_VARIABLES-DOC_NUMBER = IDOC_CONTRL-DOCNUM.
      APPEND RETURN_VARIABLES.
      RETURN_VARIABLES-WF_PARAM = APO.
      RETURN_VARIABLES-DOC_NUMBER = BELEGNUMMER.
      APPEND RETURN_VARIABLES.
      WORKFLOW_RESULT = C_WF_RESULT_OK.
    ELSE.
      RETURN_VARIABLES-WF_PARAM = EID.
      RETURN_VARIABLES-DOC_NUMBER = IDOC_CONTRL-DOCNUM.
      APPEND RETURN_VARIABLES.
      WORKFLOW_RESULT = C_WF_RESULT_ERROR.
    ENDIF.
  ELSE.
    IF ERRORCODE = 0.
      RETURN_VARIABLES-WF_PARAM = APE.
      RETURN_VARIABLES-DOC_NUMBER = BELEGNUMMER.
      APPEND RETURN_VARIABLES.
      WORKFLOW_RESULT = C_WF_RESULT_OK.
    ELSE.
      WORKFLOW_RESULT = C_WF_RESULT_ERROR.
    ENDIF.
  ENDIF.

ENDFORM.                    " RETURN_VARIABLES_FILL

Globale Daten von IDOC_INPUT_TESTER

TABLES: E1HEAD, E1ITEM.

DATA: BEGIN OF BDCDATA OCCURS 500.
        INCLUDE STRUCTURE BDCDATA.
DATA: END OF BDCDATA.

DATA: BEGIN OF XVBAK.                 "Kopfdaten
     INCLUDE STRUCTURE VBAK621.
DATA: END OF XVBAK.

DATA: BEGIN OF XVBAP OCCURS 50.        "Position
       INCLUDE STRUCTURE VBAP.
DATA:  WMENG(18) TYPE C.
DATA:  LFDAT LIKE VBAP-ABDAT.
DATA:  KSCHL LIKE KOMV-KSCHL.
DATA:  KBTRG(16) TYPE C.
DATA:  KSCHL_NETWR LIKE KOMV-KSCHL.
DATA:  KBTRG_NETWR(16) TYPE C.
DATA:  INCO1 LIKE VBKD-INCO1.
DATA:  INCO2 LIKE VBKD-INCO2.
DATA:  YANTLF(1) TYPE C.
DATA:  PRSDT LIKE VBKD-PRSDT.
DATA:  HPRSFD LIKE TVAP-PRSFD.
DATA: END OF XVBAP.

DATA: BEGIN OF DYNPRO,
      EINSTIEG          LIKE T185V-DYNNR VALUE 101,
      KKAU              LIKE T185V-DYNNR,
      UER2              LIKE T185V-DYNNR,
      KBES              LIKE T185V-DYNNR,
      ERF1              LIKE T185V-DYNNR,
      PBES              LIKE T185V-DYNNR,
      PKAU              LIKE T185V-DYNNR,
      PEIN              LIKE T185V-DYNNR,
      EID1              LIKE T185V-DYNNR,
      POPO              LIKE T185V-DYNNR,
      EIPO              LIKE T185V-DYNNR,
      KPAR              LIKE T185V-DYNNR,
      PSDE              LIKE T185V-DYNNR,
      PPAR              LIKE T185V-DYNNR,
      KDE1              LIKE T185V-DYNNR,
      KDE2              LIKE T185V-DYNNR,
      PDE1              LIKE T185V-DYNNR,
      PDE2              LIKE T185V-DYNNR,
      PKON              LIKE T185V-DYNNR,
      END OF DYNPRO.

DATA: BEGIN OF PANEL,
      KKAU              LIKE T185V-PANEL VALUE 'KKAU',
      UER2              LIKE T185V-PANEL VALUE 'UER2',
      KBES              LIKE T185V-PANEL VALUE 'KBES',
      ERF1              LIKE T185V-PANEL VALUE 'ERF1',
      PBES              LIKE T185V-PANEL VALUE 'PBES',
      PKAU              LIKE T185V-PANEL VALUE 'PKAU',
      PEIN              LIKE T185V-PANEL VALUE 'PEIN',
      EID1              LIKE T185V-PANEL VALUE 'EID1',
      EIAN              LIKE T185V-PANEL VALUE 'EIAN',
      POPO              LIKE T185V-PANEL VALUE 'POPO',
      EIPO              LIKE T185V-PANEL VALUE 'EIPO',
      KPAR              LIKE T185V-PANEL VALUE 'KPAR',
      PSDE              LIKE T185V-PANEL VALUE 'PSDE',
      POAN              LIKE T185V-PANEL VALUE 'POAN',
      PPAR              LIKE T185V-PANEL VALUE 'PPAR',
      KDE1              LIKE T185V-PANEL VALUE 'KDE1',
      KDE2              LIKE T185V-PANEL VALUE 'KDE2',
      PDE1              LIKE T185V-PANEL VALUE 'PDE1',
      PDE2              LIKE T185V-PANEL VALUE 'PDE2',
      PKON              LIKE T185V-PANEL VALUE 'PKON',
      KOAN              LIKE T185V-PANEL VALUE 'KOAN',
      END OF PANEL.

DATA: BEGIN OF ERRTAB OCCURS 20,
       TRANS  LIKE TSTC-TCODE,
       ARBGB  LIKE T100-ARBGB,
       CLASS(1) TYPE C,
       MSGNR LIKE T100-MSGNR,
*      TEXT LIKE T100-TEXT,
       TEXT(123) TYPE C,
       MSGV1 LIKE SY-MSGV1,
       MSGV2 LIKE SY-MSGV2,
       MSGV3 LIKE SY-MSGV3,
       MSGV4 LIKE SY-MSGV4,
      END OF ERRTAB.
*---- Hilfsfelder     ------------------------------------------------*

DATA: PROGRAMM_AUFTRAG LIKE T185V-AGIDV VALUE 'SAPMV45A'.

DATA: LAST_DYNPRO      LIKE T185V-DYNNR,
      WLDAT            LIKE VBAK-BSTDK,
      POSNR            LIKE VBAP-POSNR,
      FIRST(1)         TYPE C VALUE 'X'.

DATA: BEGIN OF XBDCMSGCOLL OCCURS 10.
        INCLUDE STRUCTURE BDCMSGCOLL.
DATA: END OF XBDCMSGCOLL.

* Terminauftrag  ( Auftragsart wird fest gesetzt !)
DATA:   BELEGNUMMER LIKE VBAK-VBELN.
DATA:   ERRORCODE LIKE SY-SUBRC.

* Statuswerte fuer IDOC-Status
DATA:   BELEG_NICHT_GEBUCHT LIKE TEDS1-STATUS VALUE '51'.
DATA:   BELEG_GEBUCHT       LIKE TEDS1-STATUS VALUE '53'.

*- Direktwerte für Return_variables -------------------------
data:
    eid like bdwfretvar-wf_param value 'Error_IDOCs',
    pid like bdwfretvar-wf_param value 'Processed_IDOCs',
    apo like bdwfretvar-wf_param value 'Appl_Objects',
    ape like bdwfretvar-wf_param value 'Appl_Object'.

*- Direktwerte für Workflow_Result -------------------------
DATA: C_WF_RESULT_ERROR LIKE BDWFAP_PAR-RESULT VALUE '99999'.
DATA: C_WF_RESULT_OK    LIKE BDWFAP_PAR-RESULT VALUE '0'.






Ref: http://help.sap.com/saphelp_ewm93/helpdata/en/4b/4c75954a712597e10000000a42189b/frameset.htm

Aug 8, 2012

Pricing Conditions Upload with BAPI

Below is sample code to simulate Pricing Conditions upload using BAPI


The structure of condition table A073
MANDT MANDT CLNT 3 0 Client
KAPPL KAPPL CHAR 2 0 Application
KSCHL KSCHA CHAR 4 0 Condition type
VKORG VKORG CHAR 4 0 Sales Organization
VTWEG VTWEG CHAR 2 0 Distribution Channel
MATNR MATNR CHAR 18 0 Article Number
VRKME VRKME UNIT 3 0 Sales unit
DATBI KODATBI DATS 8 0 Validity end date of the condition record
DATAB KODATAB DATS 8 0 Validity start date of the condition record
KNUMH KNUMH CHAR 10 0 Condition record number


*&---------------------------------------------------------------------*
*& Report  ZTES
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ztes.

CONSTANTS: lc_kschl_vkp0 TYPE kscha VALUE 'VKP0'.

DATA: lv_datum       TYPE sydatum,
lv_count       TYPE kopos,
lv_unit        TYPE kpein,
ls_bapicondct  TYPE bapicondct,
ls_bapicondhd  TYPE bapicondhd,
ls_bapicondit  TYPE bapicondit,
lt_bapicondct  TYPE STANDARD TABLE OF bapicondct,
lt_bapicondhd  TYPE STANDARD TABLE OF bapicondhd,
lt_bapicondit  TYPE STANDARD TABLE OF bapicondit,
lt_bapicondqs  TYPE STANDARD TABLE OF bapicondqs,
lt_bapicondvs  TYPE STANDARD TABLE OF bapicondvs,
lt_bapiret2    TYPE STANDARD TABLE OF bapiret2,
ls_bapiret2    TYPE bapiret2,
lt_bapiknumhs  TYPE STANDARD TABLE OF bapiknumhs,
lt_mem_initial TYPE STANDARD TABLE OF cnd_mem_initial.

break dc_idenny.
lv_datum = '20120812'"sy-datum.
lv_count = 1.

*LOOP AT gt_vbap INTO wa_vbap.

* BAPI Structure for Condition Tables
ls_bapicondct-operation  = '009'.
ls_bapicondct-table_no   = '073'.
ls_bapicondct-applicatio = 'V'.
ls_bapicondct-cond_usage = 'A'.
ls_bapicondct-cond_type  = lc_kschl_vkp0.

"Set the varkey from A700 table key fields
DATA lw_a073 LIKE a073.
lw_a073-VKORG = '1001'.
lw_a073-VTWEG = '03'.
lw_a073-MATNR = '000000100000072002'.
lw_a073-VRKME = 'AA1'.
CONCATENATE lw_a073-vkorg lw_a073-vtweg lw_a073-matnr lw_a073-vrkme
       INTO ls_bapicondct-varkey.
*  ls_bapicondct-varkey = space.

ls_bapicondct-valid_to   = '99991231'.
ls_bapicondct-valid_from = lv_datum.
ls_bapicondct-cond_no    = '$000000001'.

**** BAPI Structure of KONH with English Field Names
ls_bapicondhd-operation  = '009'.
ls_bapicondhd-cond_no    = '$000000001'.
ls_bapicondhd-created_by = sy-uname.
ls_bapicondhd-creat_date = sy-datum.
ls_bapicondhd-cond_usage = 'A'.
ls_bapicondhd-table_no   = '073'.
ls_bapicondhd-applicatio = 'V'.
ls_bapicondhd-cond_type  = lc_kschl_vkp0.
ls_bapicondhd-varkey     = ls_bapicondct-varkey.
ls_bapicondhd-valid_to   = '99991231'.
ls_bapicondhd-valid_from = lv_datum.

*** BAPI Structure of KONP with English Field Names
CLEAR ls_bapicondit.
ls_bapicondit-operation  = '009'.
ls_bapicondit-cond_no    = '$000000001'.
ls_bapicondit-cond_count = lv_count.
ls_bapicondit-applicatio = 'V'.
ls_bapicondit-cond_type  = lc_kschl_vkp0.
ls_bapicondit-scaletype  = 'A'.
ls_bapicondit-scalebasin = 'B'.
ADD 1 TO ls_bapicondit-scale_qty.
ls_bapicondit-calctypcon = 'B'.
lv_unit = '1'"wa_vbap-kwmeng.
ls_bapicondit-cond_p_unt = lv_unit.
ls_bapicondit-cond_value = '1.14'"wa_vbap-value.
ls_bapicondit-condcurr   = 'RM'.   "wa_vbap-waerk.

APPEND: ls_bapicondct TO lt_bapicondct,
ls_bapicondhd TO lt_bapicondhd,
ls_bapicondit TO lt_bapicondit.

*ENDLOOP.

*** BAPI for pricing Condition Records
CALL FUNCTION 'BAPI_PRICES_CONDITIONS'
  TABLES
    ti_bapicondct  = lt_bapicondct
    ti_bapicondhd  = lt_bapicondhd
    ti_bapicondit  = lt_bapicondit
    ti_bapicondqs  = lt_bapicondqs
    ti_bapicondvs  = lt_bapicondvs
    to_bapiret2    = lt_bapiret2
    to_bapiknumhs  = lt_bapiknumhs
    to_mem_initial = lt_mem_initial
  EXCEPTIONS
    update_error   = 1
    OTHERS         = 2.

IF sy-subrc EQ 0.

  WRITE: /1 'Return Messages for Condition create'(t03).
  LOOP AT lt_bapiret2 INTO ls_bapiret2.
    WRITE: /1 ls_bapiret2-message.
  ENDLOOP.
  ULINE.

  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait   = 'X'
    IMPORTING
      return = ls_bapiret2.
ENDIF.


Pricing Conditions Upload with IDOC

These are some codes to simulate Pricing Conditions upload using IDOC


*&------------------------------------------------------------------------
*&      Form  map_idoc_structure
*&------------------------------------------------------------------------
*      Map E1KOMG, E1KONH, E1KONP segment fields
*-------------------------------------------------------------------------
FORM map_idoc_structure
              USING fp_condition_table TYPE kotabnr
                    fp_rate            TYPE z_cs_cpp_rate
                    fp_condition_type  TYPE kschl
                    fp_rate_type       TYPE z_cs_file_type.

*-- Local Data Declaration
  DATA: lw_e1komg TYPE e1komg, "Filter seg. with separated condition key
        lw_e1konh TYPE e1konh, "Data from condition header
        lw_e1konp TYPE e1konp, "Conditions Items
        lv_delete TYPE konp-loevm_ko. " Delete Indicator

*-- IDoc data internal table
  DATA: i_idoc_data TYPE TABLE OF edidd
                    INITIAL SIZE 0
                    WITH HEADER LINE.

  CLEAR: lw_e1komg,
         lw_e1konh,
         lw_e1konp,
         lv_delete,
         v_segnum,
         w_e1komg,
         v_rate_type.

  REFRESH i_idoc_data.

  v_rate_type = fp_rate_type.

  PERFORM conversion_alpha_input
      USING w_crp_data-kunnr CHANGING w_crp_data-kunnr.

  PERFORM conversion_matn1_input
     USING w_crp_data-matnr CHANGING w_crp_data-matnr.

*E1KOMG: Filter segment with separated condition key
  i_idoc_data-segnam  = 'E1KOMG'.
  lw_e1komg-kvewe     =  c_usage_cond_tab.
  lw_e1komg-kotabnr   =  fp_condition_table.
  lw_e1komg-kappl     =  c_cond_type.
  lw_e1komg-kschl     =  fp_condition_type.

  CONCATENATE p_vkorg p_vtweg p_spart p_hienr p_matnr.
  CONCATENATE w_crp_data-vkorg w_crp_data-vtweg
              w_crp_data-kunnr w_crp_data-matnr
              INTO lw_e1komg-vakey.

  lw_e1komg-vakey_long = lw_e1komg-vakey.
  lw_e1komg-vkorg      = w_crp_data-vkorg.
  lw_e1komg-vtweg      = w_crp_data-vtweg.
  lw_e1komg-spart      = w_crp_data-spart.
  lw_e1komg-kunnr      = w_crp_data-kunnr.
  lw_e1komg-matnr      = w_crp_data-matnr.

  i_idoc_data-sdata = lw_e1komg.
  APPEND i_idoc_data TO i_idoc_data.

*Segment E1KONH - condition header
  i_idoc_data-segnam  = 'E1KONH'.
  lw_e1konh-datab     = w_crp_data-datab.
  lw_e1konh-datbi     = w_crp_data-datbi.
  lw_e1konh-knuma_pi  = v_promotion.
  lw_e1konh-knuma_ag  = v_sales_deal.

  i_idoc_data-sdata = lw_e1konh.
  APPEND i_idoc_data TO i_idoc_data.

  IF fm_process_type EQ c_pt_dep.
    lv_delete = c_x.              " Deletion Indicator
  ENDIF.

*Segment E1KONP - condition items
  i_idoc_data-segnam = 'E1KONP'.
  lw_e1konp-kschl     = fp_condition_type.
  lw_e1konp-kbetr     = fp_rate.
  lw_e1konp-konwa     = w_crp_data-waers.
  lw_e1konp-kpein     = v_cond_pric.
  lw_e1konp-kmein     = v_cond_unit.
  lw_e1konp-knuma_pi  = v_promotion.
  lw_e1konp-knuma_ag  = v_sales_deal.
  lw_e1konp-loevm_ko  = lv_delete.

  i_idoc_data-sdata = lw_e1konp.
  APPEND i_idoc_data TO i_idoc_data.

*-- Post Idoc for Creating Pricing Condition
  PERFORM post_idoc
         TABLES i_idoc_data.

ENDFORM.                    " map_idoc_structure

*&------------------------------------------------------------------------
*&      Form  post_idoc TABLES i_idoc_data.
*&------------------------------------------------------------------------
*      Post Idoc for Creating Pricing Condition
*------------------------------------------------------------------------
FORM post_idoc
          TABLES i_idoc_data STRUCTURE edidd.

*-- Local data declaration
  DATA: lv_komg         LIKE komg,         " Condition Structures
        lv_subrc        LIKE sy-subrc,     " Sy-subrc
        lw_messages     TYPE rsuvm_msg,    " Message Description
        lw_idoc_status  TYPE bdidocstat.   " ALE IDoc status

  DATA: idoc_contrl                        " IDoc Control data
            LIKE edidc OCCURS 1 WITH HEADER LINE.

*-- Initialize Internal Tables
  CLEAR:  t_konh,
          t_konp,
          t_konm,
          t_konw,
          i_idoc_status,
          t_kona.

  REFRESH: t_konh,
           t_konp,
           t_konm,
           t_konw,
           i_idoc_status,
           t_kona,
           i_messages.
************************************************************************
*This code is taken from IDOC_INPUT_COND_A
************************************************************************
  PERFORM fill_appl_structures(saplvkoi)
                  TABLES i_idoc_data
                         t_konh
                         t_konp
                         t_konm
                         t_konw
                         i_idoc_status
                         t_kona
                   USING lv_komg
                         lv_subrc
                         idoc_contrl-docnum
                         idoc_contrl-mestyp.
  IF lv_subrc = 0.
************************************************************************
*This code is taken from IDOC_INPUT_COND_A
************************************************************************
    PERFORM idoc_in_datenbank(saplvkoi)
              TABLES idoc_contrl
                     t_konh
                     t_konp
                     t_konm
                     t_konw
                     i_idoc_status
                     t_kona
              USING  lv_komg
                     lv_subrc
                     idoc_contrl-docnum.

    IF lv_subrc = 0.
      COMMIT WORK AND WAIT.
    Endif.