No BAPI for Purchase Order Rejection? Here is the solution, like FIORI does!

Hello,

As many of you know, to release a purchase order you can use theΒ BAPI_PO_RELEASE as the solution, just passing the PO Number and Release Strategy Code (the standard WF uses). But if you search for the “Rejection” BAPI you will not find! πŸ˜” Searching google, there are a lot of open questions about it and most of them without a solution (or giving a BDC solution)…

So based on that, i’ve created the FM below that can serve as the solution for this scenarios, using the same pieces of code that FIORI uses when rejecting the PO on the standard app.

Here it is:

function z_po_reject.
*"----------------------------------------------------------------------
*"*"Interface local:
*"  IMPORTING
*"     VALUE(I_EBELN) TYPE  EBELN
*"  TABLES
*"      T_RETURN STRUCTURE  BAPIRET2
*"----------------------------------------------------------------------
  data: lr_po           type ref to cl_po_header_handle_mm,
        ls_bapi         type bapiret2,
        l_result        type mmpur_bool,
        ls_document     type mepo_document.

*  prepare creation of PO instance
  ls_document-process     = 'PO_PROCESS'.
  ls_document-trtyp       = 'VER'.
  ls_document-doc_key(10) = i_ebeln.
  ls_document-initiator-initiator = 'RELEASE'.

  create object lr_po.
  lr_po->for_bapi = 'X'.
  lr_po->po_initialize( ls_document ).
  lr_po->set_po_number( i_ebeln ).

  try.
      lr_po->po_read( exporting im_tcode     = 'ME29N'
                                im_trtyp     = ls_document-trtyp
                                im_aktyp     = ls_document-trtyp
                                im_po_number = i_ebeln
                                im_document  = ls_document
                      importing ex_result    = l_result ).  "2212877
    catch cx_root.                                       "#EC CATCH_ALL
      l_result = mmpur_no.                                  "2212877
  endtry.

  if l_result = mmpur_no.
    "Error here...
    return.
  endif.

  if lr_po->if_releasable_mm~is_rejection_allowed( ) eq 'X'.
    lr_po->if_releasable_mm~reject(
                       exporting  im_reset = ''
                       exceptions failed   = 0 ).
    if sy-subrc eq 0.
      "Success here...
      lr_po->po_post( exceptions failure = 1
                                 others  = 2 ).
      if sy-subrc gt 0.
        lr_po->po_initialize( ).
      else.
        lr_po->po_close( ).
        free lr_po.
      endif.
    else.
      "Error here...
      call function 'BALW_BAPIRETURN_GET2'
        exporting
          type   = sy-msgty
          cl     = sy-msgid
          number = sy-msgno
          par1   = sy-msgv1
          par2   = sy-msgv2
          par3   = sy-msgv3
        importing
          return = ls_bapi.
      append ls_bapi to t_return.
    endif.
  endif.
endfunction.

Testing:

Running:

Rejected:

Enjoy it! πŸŽ―πŸ†

Regards.

Original Article:
https://blogs.sap.com/2020/03/13/no-bapi-for-purchase-order-rejection-here-is-the-solution-like-fiori-does/

ASK SAP EXPERTS ONLINE
Related blogs

LEAVE A REPLY

Please enter your comment!
Please enter your name here