Friday, 20 June 2014

2.BAPI- How to Use Standard BAPI

Business Scenario: Creating Sales Orders by Using Standard BAPI Function Module.

Usually in Organizations sales orders are created on every day basis based on the customer orders but the billing/invoices are generated at the end of the month. The billing Docs are generated in Tx- VF04 by collective billing process which picks all the sales orders and invoices are generated and this collective billing process runs by  some Background job.

In some development/test  scenario  to test  the Collective billing process they need bunch of sales documents. In this case to create sales orders one by one by Tx- VA01 is hectic. so in this case calling the BAPI FM to create sales orders or any BDC program for the same  is really time saving. The below Post demonstrates how to use a standard BAPI to create Sales order.

_________________________________________________________________________________
Step1.  Go to Tx- VA01.





Step2. Provide sales order type and other details.



















Step3. Provide the sold to party, pur order number and material , quantity. Go to the Header level.















Step4. Under partners tab we can see all partners.












Step5. Now save it and a sales order number is generated.


















Step6. Now go to Tx- SE11 & open the table TVAK where all the document types are defined.













Step7. For our case the document type is OR.












Step8. In the Back end OR appears as TA.







Step9. Go to Tx- BAPI and we can find the proper BAPI  FM that is needed to create Sales Order.





















_________________________________________________________________________________

Step10. Create the below report and call the BAPI FM & pass the header, partner, item and schedule line data and execute it.
_________________________________________________________________________________
REPORT  zbapi_so_create.

PARAMETERS : p_no_so TYPE int4. " no of sales orders to be created

DATA :  ls_ord_hdr     TYPE bapisdhd1,
               ls_ord_hdrx   TYPE bapisdhd1x,
               so_num          TYPE bapivbeln-vbeln,
               lt_so_num      LIKE TABLE OF so_num,
               lt_ret             TYPE TABLE OF bapiret2,
               lt_item           TYPE TABLE OF bapisditm,
               ls_item          TYPE bapisditm,
               lt_itemx         TYPE TABLE OF bapisditmx,
               ls_itemx         TYPE bapisditmx,
               lt_part            TYPE TABLE OF bapiparnr,
               ls_part           TYPE  bapiparnr,
               lt_schd           TYPE TABLE OF bapischdl ,
               ls_schd          TYPE bapischdl,
               lt_schdx         TYPE TABLE OF  bapischdlx,
               ls_schdx        TYPE bapischdlx.

START-OF-SELECTION.

******* Fill the Sales Order Header ****************
  ls_ord_hdr-doc_type  = 'TA'. " ORDER Type : OR->TA as per TVAK table
  ls_ord_hdr-sales_org  = 'AB99'.
  ls_ord_hdr-distr_chan  = 'WS'.
  ls_ord_hdr-division  = 'LT'.
  ls_ord_hdr-purch_no_c  = 'ORDER_100'.

  ls_ord_hdrx-updateflag = 'X'.
  ls_ord_hdrx-doc_type ='X'.
  ls_ord_hdrx-sales_org = 'X'.
  ls_ord_hdrx-distr_chan = 'X'.
  ls_ord_hdrx-division = 'X'.
  ls_ord_hdrx-purch_no_c = 'X'.

*********FILL PARTNERS TABLE  ******************
  ls_part-partn_role = 'AG'.  " SOLD TO PARTY
  ls_part-partn_numb = '0000099994'.
  APPEND ls_part TO lt_part.
  CLEAR ls_part.


  ls_part-partn_role = 'RE'.  " BILL TO PARTY
  ls_part-partn_numb = '0000099996'.
  APPEND ls_part TO lt_part.
  CLEAR ls_part.


  ls_part-partn_role = 'RG'.  " PAYER
  ls_part-partn_numb = '0000099997'.
  APPEND ls_part TO lt_part.
  CLEAR ls_part.

  ls_part-partn_role = 'WE'.  " SHIP TO PARTY
  ls_part-partn_numb = '0000099995'.
  APPEND ls_part TO lt_part.
  CLEAR ls_part.

*********FILL ITEMS ***********************
  ls_item-itm_number = '10'.
  ls_item-material = '000000000000001278'.
  ls_item-plant = 'PLBN'.
  ls_item-target_qty = '10'.
  ls_item-target_qu = 'EA'.
  APPEND ls_item TO lt_item.
  CLEAR : ls_item.

  ls_itemx-itm_number = '10'.
  ls_itemx-material = 'X'.
  ls_itemx-plant = 'X'.
  ls_itemx-target_qty = 'X'.
  ls_itemx-target_qu = 'X'.
  APPEND ls_itemx  TO lt_itemx.
  CLEAR : ls_itemx.
******* SCHEDULE LINE *********************

  ls_schd-itm_number = '10'.
  ls_schd-req_qty = '10'.
  APPEND ls_schd TO lt_schd.
  CLEAR ls_schd.


  ls_schdx-itm_number = '10'.
  ls_schdx-req_qty = 'X'.
  ls_schdx-updateflag = 'X'.
  APPEND ls_schdx TO lt_schdx.
  CLEAR ls_schdx.

  IF p_no_so > 0.
    DO p_no_so TIMES.
      CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
        EXPORTING
          order_header_in     = ls_ord_hdr
          order_header_inx    = ls_ord_hdrx
        IMPORTING
          salesdocument       = so_num
        TABLES
          return              = lt_ret
          order_items_in      = lt_item
          order_items_inx     = lt_itemx
          order_partners      = lt_part
          order_schedules_in  = lt_schd
          order_schedules_inx = lt_schdx.
      IF sy-subrc = 0.
        APPEND so_num TO lt_so_num.
        CLEAR so_num.
        COMMIT WORK.
      ENDIF.
    ENDDO.
  ENDIF.

END-OF-SELECTION.
  IF lt_so_num IS NOT INITIAL.
    LOOP AT lt_so_num INTO so_num.
      WRITE :/ 'Sales Order Generated By BAPI call-', sy-tabix , ':', so_num.
    ENDLOOP.
  ENDIF.
_________________________________________________________________________________

Step11. Provie a number which indicates how much Sales Order to be created.










Step12. The BAPI generates the below sales orders.














Step13. we can view the sales order in the Tx- VA03.






Step14. Provide the above created Sales Order number and hit Enter key.



















Step15. We can see the details of the Sales Order created by the BAPI FM call.


















--------------------------------xxxx-------------------xxxx---------------------xxxx------------------------------------

No comments:

Comments system

Disqus Shortname