Thursday 19 June 2014

1.BAPI- How to Use Standard BAPI

_________________________________________________________________________________
Business Scenario: Suppose in an Organization for some Maintenance activity they need a Grinder Machine . It might be the case that the Machine is already there and they can reserve the machine for some day(why reservation of  the machine like in a big org they can have multiple maintenance activities and some of them may need the same machine) but what if the Machine is not there. That time the Org takes the decision to get it from Vendors for which the Purchasing dept should raise a Purchase requisition ,then Purchase order and at last the vendor supplies the goods.

In SAP R/3 the issue can be replicated by Creating a Reservation(Tx-MB21) which says that there is a requirement for some material and then they go to Tx-ME51N and Create the Purchase Requisition and then Pur Order . This all needs front end interaction with the SAP GUI.

Here the Org can take the Decision that when ever there is any requirement for a Material the Purchase Requisition should generate automatically and also the Purchase Order by some background job.
In such case we can use the Standard BAPI: 'BAPI_PR_CREATE' to generate the Purchase Requisition from the Reservation .
_________________________________________________________________________________

Step1. Go to Tx- MB21 to create a reservation of material.















Step2. Provide the details and click on Create Button.
















Step3. Provide the cost center and Material number, quantity, plant and storage location. It signifies that this plant needs material for the Maintenance activity.
















Step4. Save it.


















Step5. Teh Reservation Number is generated.




















Step6. The Reservations are stored in the Table: RESB.






Step7. Then the User Run the Tx: ME51N to cretae a purchase requisition for the required material quantity .












Step8. Pur Req- Doc type is NB. Provides the material number and quantity and creates a purchase requisition.






















Step9. The below Pur Requisition is generated which can be displayed in ME53N.


















Step10. Lets Automate the process by Using BAPI fm. The Bapi: BAPI_PR_CRETAE can be used to create the bapi in  background.



















Step11. Create a message class to show our message.













_________________________________________________________________________________
Step12. Below report creates the Purchase requisition for the selected Reservation.

REPORT  zres_pr_create MESSAGE-ID ZPR_CRT.

TYPE-POOLS : slis.
DATA :             lt_resb TYPE TABLE OF resb.

START-OF-SELECTION.
  SELECT * FROM resb INTO TABLE lt_resb WHERE bdter = sy-datum. " Get all reservation 
                                                                                                                      " created on the current date
  SORT lt_resb BY rsnum rspos.

END-OF-SELECTION.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program       = sy-cprog
      i_callback_pf_status_set = 'FM_STATUS'
      i_callback_user_command  = 'CRT_PR'
      i_structure_name         = 'RESB'
      i_grid_title             = 'Showing Todays Material Reservation'
    TABLES
      t_outtab                 = lt_resb.

*&---------------------------------------------------------------------*
*&      Form  FM_status
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM fm_status USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS 'STATUS_PR' EXCLUDING rt_extab. " Create Pf status for appl toolbar Button
ENDFORM.                    "fm_status

*&---------------------------------------------------------------------*
*&      Form  crt_pr
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->R_UCOMM      text
*      -->RS_SELFIELD  text
*----------------------------------------------------------------------*
FORM crt_pr USING r_ucomm LIKE sy-ucomm
                                   rs_selfield TYPE slis_selfield.
  DATA : it_resb     TYPE TABLE OF resb,
                ls_resb    TYPE resb,
                pos          TYPE rspos .
  DATA : ls_hdr           TYPE bapimereqheader,
               ls_hdr_x        TYPE bapimereqheaderx,
               pr_num          TYPE bapimereqheader-preq_no,
               t_ret               TYPE TABLE OF bapiret2,
               s_ret              TYPE  bapiret2,
               t_pritm          TYPE TABLE OF bapimereqitemimp,
               s_pritm         TYPE  bapimereqitemimp,
               t_pritmx        TYPE TABLE OF bapimereqitemx,
               s_pritmx        TYPE  bapimereqitemx.

  IF rs_selfield-sel_tab_field  = '1-RSNUM'. " CHeck if the User Single-click on the Reservation No.

    LOOP AT lt_resb INTO ls_resb WHERE rsnum = rs_selfield-value . " Read the Reservation Value
      APPEND ls_resb TO it_resb.
      CLEAR ls_resb.
    ENDLOOP.

** The Reservation Records are Ready in the Internal Table it_resb**

** FILL Header ****
    ls_hdr-pr_type = 'NB'.   " Pur Requisition Document type
    ls_hdr_x-pr_type = 'X'.
****fill item***
    LOOP AT it_resb INTO ls_resb .
      pos                              = 10 + ls_resb-rspos - 1.
      s_pritm-preq_item      = pos.
      s_pritm-pur_group      = '011'.               " pur group
      s_pritm-preq_name     = 'SHIPPING'.   " requisitioner name
      s_pritm-material          = ls_resb-matnr. " material Number
      s_pritm-plant               = ls_resb-werks. " plant number
      s_pritm-store_loc        = ls_resb-lgort.   " storage loc
      s_pritm-quantity          = ls_resb-bdmng. " quantity
      s_pritm-unit                 = ls_resb-meins. " unit
      s_pritm-deliv_date      = sy-datum + 10 . " delivery date of goods
      s_pritm-reserv_no       = ls_resb-rsnum.  " reservation number
      APPEND s_pritm TO t_pritm.
      CLEAR : ls_resb, s_pritm.
*****fill item text ***
      s_pritmx-preq_item      = pos.    
      s_pritmx-preq_itemx    = 'X'.
      s_pritmx-pur_group      = 'X'.
      s_pritmx-preq_name     = 'X'.
      s_pritmx-material          = 'X'.
      s_pritmx-plant              = 'X'.
      s_pritmx-store_loc       = 'X'.
      s_pritmx-quantity          = 'X'.
      s_pritmx-unit                = 'X'.
      s_pritmx-deliv_date     = 'X'.
      APPEND s_pritmx TO t_pritmx.
      CLEAR s_pritmx.
    ENDLOOP.

    CASE r_ucomm.

      WHEN 'CRT_PR'. " Fcode when the user clicks on the Appl Tool Bar Button "Create Pur Req" .
        CALL FUNCTION 'BAPI_PR_CREATE'  " Call this bapi and pass the values
          EXPORTING
            prheader  = ls_hdr
            prheaderx = ls_hdr_x
          IMPORTING
            number    = pr_num
          TABLES
            return    = t_ret  " return message is store in this table
            pritem    = t_pritm
            pritemx   = t_pritmx.
        IF sy-subrc = 0.
          READ TABLE t_ret INTO s_ret WITH KEY type = 'S'.
          IF sy-subrc  = 0.
            MESSAGE  I000 WITH s_ret-message_v1 rs_selfield-value.
            COMMIT WORK.
          ENDIF.
        ELSE.
          ROLLBACK WORK.
        ENDIF.
    ENDCASE.
  ENDIF.
ENDFORM. "CRT_PR


_________________________________________________________________________________

Step13. Create the PF status with below application tool bar Button.
















Step14. Run the report and we can see all the Reservations created for the current date.










Step15. Select any one and click on the Button Cretae Pur Requisition.













Step16. The BAPI is executed and the Pur Req. is generated which can be viewed in Tx- ME53N.


















_________________________________________________________________________________

No comments:

Comments system

Disqus Shortname