Register Login

BAPI for Sales Order creation.

Updated May 18, 2018

                                                                     
                                                                     
                                                                     
                                             
*&---------------------------------------------------------------------*
*& Report  ZBC2_TRG05_PRG30
*&
*&---------------------------------------------------------------------*
*&
*&***BAPI for sales order creation***
*&---------------------------------------------------------------------*

REPORT  zbc2_trg05_prg30.
TYPES : BEGIN OF ty_main,
        f1(10),
        f2(10),
        f3(10),
        f4(10),
        f5(10),
        f6(10),
  END OF ty_main.

TYPES : BEGIN OF ty_head,
        salesdocument TYPE bapivbeln-vbeln,
        doc_type TYPE bapisdh1-collect_no,
          sales_org TYPE bapisdh1-sales_org,
       distr_chan TYPE bapisdh1-distr_chan,
       division TYPE bapisdh1-division,
   req_date_h TYPE bapisdh1-req_date_h,
  END OF ty_head.

TYPES :BEGIN OF ty_item,
  salesdocument TYPE bapivbeln-vbeln,
  itm_number TYPE bapisditm-itm_number,
  material TYPE bapisditm-material,
  target_qty TYPE bapisditm-target_qty,
  target_qu TYPE bapisditm-target_qu,
  plant TYPE bapisditm-plant,
  itm_number1 TYPE bapischdl-itm_number,
  req_qty TYPE bapischdl-req_qty,
  END OF ty_item.

DATA :it_order_item_in TYPE bapisditm OCCURS 0 WITH HEADER LINE,
      it_order_item_inx TYPE bapisditmx OCCURS 0 WITH HEADER LINE,
      it_partners TYPE bapiparnr OCCURS 0 WITH HEADER LINE ,
      it_return TYPE bapiret2 OCCURS 0 WITH HEADER LINE,
      it_order_schedules_in TYPE bapischdl OCCURS 0 WITH HEADER LINE,
      it_order_schedules_inx TYPE bapischdlx OCCURS 0 WITH HEADER LINE,
      it_main TYPE ty_main OCCURS 0 WITH HEADER LINE ,
       it_main1 TYPE ty_main OCCURS 0 WITH HEADER LINE,
       it_head TYPE ty_head OCCURS 0 WITH HEADER LINE,
       it_item TYPE ty_item OCCURS 0 WITH HEADER LINE.

PARAMETERS : p_file TYPE rlgrap-filename.
DATA: c_file TYPE string,
      wa_order_header_in LIKE bapisdhd1,
      wa_order_header_inx LIKE bapisdhd1x,
      c_vbeln TYPE vbak-vbeln,
      message_text_output TYPE natxt.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      program_name  = syst-cprog
      dynpro_number = syst-dynnr
      field_name    = ' '
    IMPORTING
      file_name     = p_file.

START-OF-SELECTION.
  c_file = p_file.
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                      = c_file
     filetype                      = 'ASC'
     has_field_separator           = 'X'
*           HEADER_LENGTH                 = 0
*           READ_BY_LINE                  = 'X'
*           DAT_MODE                      = ' '
*           CODEPAGE                      = ' '
*           IGNORE_CERR                   = ABAP_TRUE
*           REPLACEMENT                   = '#'
*           CHECK_BOM                     = ' '
*           VIRUS_SCAN_PROFILE            =
*           NO_AUTH_CHECK                 = ' '
*         IMPORTING
*           FILELENGTH                    =
*           HEADER                        =
    TABLES
      data_tab                      = it_main[]
   EXCEPTIONS
     file_open_error               = 1
     file_read_error               = 2
     no_batch                      = 3
     gui_refuse_filetransfer       = 4
     invalid_type                  = 5
     no_authority                  = 6
     unknown_error                 = 7
     bad_data_format               = 8
     header_not_allowed            = 9
     separator_not_allowed         = 10
     header_too_long               = 11
     unknown_dp_error              = 12
     access_denied                 = 13
     dp_out_of_memory              = 14
     disk_full                     = 15
     dp_timeout                    = 16
     OTHERS                        = 17
            .
  IF sy-subrc  0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.


  LOOP AT it_main .
    MOVE-CORRESPONDING it_main TO  it_main1.
    AT NEW f1.
      it_head-salesdocument = it_main1-f1.
      it_head-doc_type = it_main1-f2.
      it_head-sales_org = it_main1-f3.
      it_head-distr_chan = it_main1-f4.
      it_head-division = it_main1-f5.
      it_head-req_date_h = it_main1-f6.
      APPEND it_head.
      CONTINUE.
    ENDAT.
    it_item-salesdocument = it_head-salesdocument.
    it_item-itm_number = it_main1-f2.
    it_item-material = it_main1-f3.
    it_item-target_qty = it_main1-f4.
    it_item-target_qu =  it_main1-f5.
    it_item-plant = it_main1-f6.
    APPEND it_item.

  ENDLOOP.

  LOOP AT it_head.
    wa_order_header_in-doc_type = it_head-doc_type.
    wa_order_header_in-sales_org = it_head-sales_org.
    wa_order_header_in-distr_chan = it_head-distr_chan.
    wa_order_header_in-division = it_head-division.
    wa_order_header_in-req_date_h = it_head-req_date_h.

    wa_order_header_inx-updateflag = 'I'.
    wa_order_header_inx-doc_type = 'X'.
    wa_order_header_inx-sales_org = 'X'.
    wa_order_header_inx-distr_chan = 'X'.
    wa_order_header_inx-division = 'X'.
    wa_order_header_inx-req_date_h = 'X'.

    LOOP AT it_item WHERE salesdocument = it_head-salesdocument.
      it_order_item_in-itm_number = it_item-itm_number.
      it_order_item_in-material = it_item-material.
      it_order_item_in-target_qty = it_item-target_qty.
      it_order_item_in-target_qu = it_item-target_qu.
      it_order_item_in-plant = it_item-plant.
      APPEND it_order_item_in.

      it_order_item_inx-itm_number = it_item-itm_number.
      it_order_item_inx-updateflag ='I'.
      it_order_item_inx-material = 'X'.
      it_order_item_inx-target_qty = 'X'.
      it_order_item_inx-target_qu = 'X'.
      it_order_item_inx-plant = 'X'.
      APPEND it_order_item_inx.

      it_order_schedules_in-itm_number =  it_order_item_in-itm_number.
      it_order_schedules_in-req_qty = it_order_item_in-target_qty.
      APPEND it_order_schedules_in.

      it_order_schedules_inx-itm_number = it_order_item_in-itm_number.
      it_order_schedules_inx-req_qty = 'X'.
      APPEND it_order_schedules_inx.

    ENDLOOP.
    it_partners-partn_role = 'AG'.
    it_partners-partn_numb = '0000002004'.
    APPEND it_partners.
    it_partners-partn_role = 'WE'.
    it_partners-partn_numb = '0000002004'.
    APPEND it_partners.

    CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
      EXPORTING
*   SALESDOCUMENTIN               =
        order_header_in               = wa_order_header_in
       order_header_inx              = wa_order_header_inx
*   SENDER                        =
*   BINARY_RELATIONSHIPTYPE       =
*   INT_NUMBER_ASSIGNMENT         =
*   BEHAVE_WHEN_ERROR             =
*   LOGIC_SWITCH                  =
*   TESTRUN                       =
       convert                       = 'X'
    IMPORTING
       salesdocument                 = c_vbeln
      TABLES
       return                        =  it_return[]
       order_items_in                = it_order_item_in[]
       order_items_inx               = it_order_item_inx[]
        order_partners                = it_partners[]
       order_schedules_in            = it_order_schedules_in[]
       order_schedules_inx           = it_order_schedules_inx[]
*   ORDER_CONDITIONS_IN           =
*   ORDER_CONDITIONS_INX          =
*   ORDER_CFGS_REF                =
*   ORDER_CFGS_INST               =
*   ORDER_CFGS_PART_OF            =
*   ORDER_CFGS_VALUE              =
*   ORDER_CFGS_BLOB               =
*   ORDER_CFGS_VK                 =
*   ORDER_CFGS_REFINST            =
*   ORDER_CCARD                   =
*   ORDER_TEXT                    =
*   ORDER_KEYS                    =
*   EXTENSIONIN                   =
*   PARTNERADDRESSES              =
              .


*LOOP AT it_return where type eq 'E' or type eq 'A'.
*  message : it_return-message type 'S'.
*exit.
*endloop.
*IF sy-subrc ne 0.
*
*  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
*   EXPORTING
*     WAIT          = 'X'
**   IMPORTING
**     RETURN        =
*            .
*
*write : 'SO created:',c_vbeln.
*ENDIF.

    READ TABLE it_return WITH KEY type = 'E'.
    IF sy-subrc EQ 0.
      CALL FUNCTION 'MESSAGE_TEXT_BUILD'
        EXPORTING
          msgid               = it_return-id
          msgnr               = it_return-number
          msgv1               = it_return-message_v1
          msgv2               = it_return-message_v2
          msgv3               = it_return-message_v3
          msgv4               = it_return-message_v4
        IMPORTING
          message_text_output = message_text_output.
      WRITE :message_text_output.
    else.
       CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
   EXPORTING
     WAIT          = 'X'
*   IMPORTING
*     RETURN        =
            .
data : lineno type sy-linno value 1.
      lineno = lineno + 1.
      sy-linno = lineno.
      skip.
write : /3 'SO created for:',c_vbeln.
    ENDIF.


    CLEAR :wa_order_header_in,wa_order_header_inx,it_order_item_in[],it_order_item_inx[],it_head,
            it_order_schedules_in[],it_order_schedules_inx[].
  ENDLOOP.




13001	OR	1000	12	00	20110620
13001	10	M-05	25	ST	1200
13001	20	M-06	35	ST	1200
13001	30	M-07	15	ST	1200
13002	OR	1000	12	00	20110621
13002	10	M-05	44	ST	1200
13002	20	M-06	33	ST	1200








×