Create PO after fully approval of PR using workflows


                                                               REFER - PR Approval

-------------------------------------------------------Changing custom class-------------------------------------------------------------------

Step 1: Continue from the above blogspot.

Step 2: Create a method PO_CREATE with import and export parameters.


Step 3: Write code.

  METHOD po_create.
    DATA:
      l_t_items_pr  TYPE TABLE OF bapieban,
      l_r_items_pr  TYPE  bapieban,
      l_t_acc_pr    TYPE TABLE OF bapiebkn,
      l_r_acc_pr    TYPE  bapiebkn,
      l_t_text      TYPE TABLE OF bapiebantx,
      l_r_text      TYPE bapiebantx,
      l_t_limits    TYPE TABLE OF bapiesuhc,
      l_r_limits    TYPE bapiesuhc,
      l_t_cnrt_lim  TYPE TABLE OF bapiesuc,
      l_r_cnrt_lim  TYPE  bapiesuc,
      l_t_ser       TYPE TABLE OF bapiesll,
      l_r_ser       TYPE bapiesll,
      l_t_ser_text  TYPE TABLE OF bapieslltx,
      l_r_ser_text  TYPE bapieslltx,
      l_t_acc_val   TYPE TABLE OF bapieskl,
      l_r_acc_val   TYPE bapieskl,

*header PO
      l_r_header_x  TYPE bapimepoheaderx,
      l_r_header    TYPE bapimepoheader,
*account details PO
      l_r_acc       TYPE bapimepoaccount,
      l_t_acc       TYPE STANDARD TABLE OF bapimepoaccount,
      l_r_acc_x     TYPE bapimepoaccountx,
      l_t_acc_x     TYPE STANDARD TABLE OF bapimepoaccountx,
      l_f_acc(1)    TYPE c,
*item details PO
      l_r_items     TYPE bapimepoitem,
      l_t_items     TYPE STANDARD TABLE OF bapimepoitem,
      l_r_items_x   TYPE bapimepoitemx,
      l_t_items_x   TYPE STANDARD TABLE OF bapimepoitemx,
*PO schedule
      l_r_sched     TYPE bapimeposchedule,
      l_t_sched     TYPE STANDARD TABLE OF bapimeposchedule,
      l_r_sched_x   TYPE bapimeposchedulx,
      l_t_sched_x   TYPE STANDARD TABLE OF bapimeposchedulx,
*po address delivery
      l_r_addr_delv TYPE bapimepoaddrdelivery,
      l_t_addr_delv TYPE TABLE OF bapimepoaddrdelivery,
*return
      l_t_return    TYPE STANDARD TABLE OF bapireturn,
      l_t_return2    TYPE STANDARD TABLE OF bapiret2,
*exception
      l_r_msg_key   TYPE scx_t100key,
* partner
      l_t_partner   TYPE TABLE OF bapiekkop,
      l_r_partner   TYPE bapiekkop,
* limits
      l_t_limits1   TYPE TABLE OF bapiesuh,
      l_r_limits1   TYPE  bapiesuh.
    CONSTANTSl_c_x TYPE VALUE 'X'.
    l_f_acc 'K'.
    TRY.
        CALL FUNCTION 'BAPI_REQUISITION_GETDETAIL'
          EXPORTING
            number                         i_f_banfn
            account_assignment             l_f_acc
            services                       l_c_x
          TABLES
            requisition_items              l_t_items_pr
            requisition_account_assignment l_t_acc_pr
            requisition_text               l_t_text
            requisition_limits             l_t_limits1
            requisition_contract_limits    l_t_cnrt_lim
            requisition_services           l_t_ser
            requisition_services_texts     l_t_ser_text
            requisition_srv_accass_values  l_t_acc_val
            return                         l_t_return.
      CATCH cx_sy_dyn_call_illegal_type INTO DATA(lr_exception).
        WRITE'Exception:'lr_exception->get_text).
    ENDTRY.
    l_r_items_pr l_t_items_pr[ ].
*company code
    l_r_header-comp_code 'Y100'.
    l_r_header_x-comp_code l_c_x.
*creation date
    l_r_header-creat_date sy-datum.
    l_r_header_x-creat_date l_c_x.
*document type
    IF l_r_items_pr-doc_type IS NOT INITIAL.
      l_r_header-doc_type l_r_items_pr-doc_type.
      l_r_header_x-doc_type l_c_x.
    ENDIF.
*created by
    IF l_r_items_pr-created_by IS NOT INITIAL.
      l_r_header-created_by l_r_items_pr-created_by.
      l_r_header_x-created_by l_c_x.
    ENDIF.
*desired vendor
    IF l_r_items_pr-des_vendor IS NOT INITIAL.
      l_r_header-vendor   l_r_items_pr-des_vendor.
      l_r_header_x-vendor l_c_x.
    ENDIF.
* Fixed Supplier
    IF l_r_items_pr-fixed_vend IS NOT INITIAL.
      l_r_header-vendor   l_r_items_pr-fixed_vend.
      l_r_header_x-vendor l_c_x.
    ENDIF.
* Purchase organisation
    IF l_r_items_pr-purch_org IS NOT INITIAL.
      l_r_header-purch_org l_r_items_pr-purch_org.
      l_r_header_x-purch_org l_c_x.
    ENDIF.
* Payment terms
    IF l_r_header-vendor IS NOT INITIAL .
      SELECT SINGLE lifnr,parvw,lifn2
                FROM wyt3
                INTO @DATA(l_r_inv_party)
                WHERE lifnr @l_r_header-vendor AND
                      ekorg @l_r_items_pr-purch_org AND
*                        parvw IN ( 'RS' , 'LF' ).  " RS--10 digit vendor & LF--8 digit vendor.
                  parvw 'LF'.
      IF l_r_inv_party-lifn2 IS NOT INITIAL.
        SELECT SINGLE lifnr,
                      zterm,
                      xersy
                  FROM lfm1
                  INTO @DATA(l_r_lfm1)
                  WHERE lifnr EQ @l_r_inv_party-lifn2.
        IF l_r_lfm1-zterm IS NOT INITIAL.
          l_r_header-pmnttrms l_r_lfm1-zterm.
          l_r_header_x-pmnttrms l_c_x.
        ENDIF.
      ENDIF.
    ENDIF.
*purchase group
    IF l_r_items_pr-pur_group IS NOT INITIAL.
      l_r_header-pur_group   l_r_items_pr-pur_group.
      l_r_header_x-pur_group l_c_x.
    ENDIF.
*currency
    IF l_r_items_pr-currency IS NOT INITIAL.
      l_r_header-currency    =  l_r_items_pr-currency.
      l_r_header_x-currency  =  l_c_x.
    ENDIF.
*doc date
    l_r_header-doc_date      sy-datum.
    l_r_header_x-doc_date    l_c_x.
*po account
    LOOP AT l_t_acc_pr ASSIGNING FIELD-SYMBOL(<l_r_acc_pr>).
      IF <l_r_acc_pr>-delete_ind NE abap_true.
*item
        IF <l_r_acc_pr>-preq_item IS NOT INITIAL.
          l_r_acc-po_item      <l_r_acc_pr>-preq_item.
          l_r_acc_x-po_item    <l_r_acc_pr>-preq_item.
        ENDIF.
*quantity
        IF <l_r_acc_pr>-preq_qty IS NOT INITIAL.
          l_r_acc-quantity     <l_r_acc_pr>-preq_qty.
          l_r_acc_x-quantity   l_c_x.
        ENDIF.
*g_l_account
        IF <l_r_acc_pr>-g_l_acct IS NOT INITIAL.
          l_r_acc-gl_account   <l_r_acc_pr>-g_l_acct.
          l_r_acc_x-gl_account l_c_x.
        ENDIF.
*cost center
        IF <l_r_acc_pr>-cost_ctr IS NOT INITIAL.
          l_r_acc-costcenter   <l_r_acc_pr>-cost_ctr.
          l_r_acc_x-costcenter l_c_x.
        ENDIF.

        APPEND l_r_acc   TO l_t_acc.
        APPEND l_r_acc_x TO l_t_acc_x.
        CLEAR l_r_accl_r_acc_x.
      ENDIF.
    ENDLOOP.
*po_item details
    LOOP AT l_t_items_pr ASSIGNING FIELD-SYMBOL(<l_r_items_pr>).
      IF <l_r_items_pr>-delete_ind EQ abap_true.
        DELETE l_t_acc WHERE po_item EQ <l_r_items_pr>-preq_item.
        DELETE l_t_acc_x WHERE po_item EQ <l_r_items_pr>-preq_item.
      ELSE.
*item
        IF <l_r_items_pr>-preq_item IS NOT INITIAL.
          l_r_items-po_item      =  <l_r_items_pr>-preq_item.
          l_r_items_x-po_item    =  <l_r_items_pr>-preq_item.
        ENDIF.
*short text
        IF <l_r_items_pr>-short_text IS NOT INITIAL.
          l_r_items-short_text   <l_r_items_pr>-short_text.
          l_r_items_x-short_text l_c_x.
        ENDIF.
*plant
        IF <l_r_items_pr>-plant IS NOT INITIAL.
          l_r_items-plant        <l_r_items_pr>-plant.
          l_r_items_x-plant      =  l_c_x.
        ENDIF.
*storage location
        IF <l_r_items_pr>-store_loc IS NOT INITIAL .
          l_r_items-stge_loc     <l_r_items_pr>-store_loc.
          l_r_items_x-stge_loc   =  l_c_x.
        ENDIF.
*material group
        IF <l_r_items_pr>-mat_grp IS NOT INITIAL.
          l_r_items-matl_group   <l_r_items_pr>-mat_grp.
          l_r_items_x-matl_group =  l_c_x.
        ENDIF.
*quantity
        IF <l_r_items_pr>-quantity IS NOT INITIAL.
          l_r_items-quantity     <l_r_items_pr>-quantity.
          l_r_items_x-quantity   =  l_c_x.
        ENDIF.
*unit
        IF <l_r_items_pr>-unit IS NOT INITIAL .
          l_r_items-po_unit      <l_r_items_pr>-unit.
          l_r_items_x-po_unit    =  l_c_x.
        ENDIF.
*ORDERPR_UN
        IF <l_r_items_pr>-c_amt_bapi IS NOT INITIAL.
*net price
          l_r_items-net_price    <l_r_items_pr>-c_amt_bapi.
          l_r_items_x-net_price  =  l_c_x.
        ENDIF.
*Unit Price
        IF <l_r_items_pr>-price_unit IS NOT INITIAL.
          l_r_items-price_unit    <l_r_items_pr>-price_unit.
          l_r_items_x-price_unit  =  l_c_x.
        ENDIF.
* to consider Price per unit and PO price from PR instead contract
        l_r_items-po_price    '2'.
        l_r_items_x-po_price  =  l_c_x.
*account
        IF <l_r_items_pr>-acctasscat IS NOT INITIAL.
          l_r_items-acctasscat   <l_r_items_pr>-acctasscat.
          l_r_items_x-acctasscat =  l_c_x.
        ENDIF.
*preq number
        IF <l_r_items_pr>-preq_no IS NOT INITIAL.
          l_r_items-preq_no      <l_r_items_pr>-preq_no.
          l_r_items_x-preq_no    =  l_c_x.
        ENDIF.
*item
        IF <l_r_items_pr>-preq_item IS NOT INITIAL.
          l_r_items-preq_item    <l_r_items_pr>-preq_item.
          l_r_items_x-preq_item  =  l_c_x.
        ENDIF.
*reference doc
        IF <l_r_items_pr>-preq_no IS NOT INITIAL.
          l_r_items-ref_doc      <l_r_items_pr>-preq_no.
          l_r_items_x-ref_doc    =  l_c_x.
        ENDIF.
*Package Number
        IF l_r_items_pr-pckg_no IS NOT INITIAL.
          l_r_items-pckg_no   <l_r_items_pr>-pckg_no.
          l_r_items_x-pckg_no =  l_c_x.
        ENDIF.
*Item Category
        IF l_r_items_pr-item_cat IS NOT INITIAL.
          l_r_items-item_cat <l_r_items_pr>-item_cat.
          l_r_items_x-item_cat l_c_x.
        ENDIF.
*ERS flag
        IF l_r_inv_party-lifn2 IS NOT INITIAL.
          l_r_items-ers l_r_lfm1-xersy .
          l_r_items_x-ers l_c_x.
        ENDIF.
*Tax code
        l_r_items-tax_code 'V3'.
        l_r_items_x-tax_code l_c_x.

        APPEND l_r_items TO l_t_items.
        APPEND l_r_items_x TO l_t_items_x.
*po schedule
        IF <l_r_items_pr>-preq_item IS NOT INITIAL.
          l_r_sched-po_item      <l_r_items_pr>-preq_item.
          l_r_sched_x-po_item    <l_r_items_pr>-preq_item.
        ENDIF.
*delivery date
        IF <l_r_items_pr>-deliv_date IS NOT INITIAL.
          l_r_sched-delivery_date <l_r_items_pr>-deliv_date.
          l_r_sched_x-delivery_date l_c_x.
        ENDIF.
*quantity
        IF <l_r_items_pr>-quantity IS NOT INITIAL.
          l_r_sched-quantity    <l_r_items_pr>-quantity.
          l_r_sched_x-quantity  l_c_x.
        ENDIF.
        APPEND l_r_sched TO l_t_sched.
        APPEND l_r_sched_x TO l_t_sched_x.
*po address delivery
        IF <l_r_items_pr>-preq_item IS NOT INITIAL.
          l_r_addr_delv-po_item <l_r_items_pr>-preq_item.
        ENDIF.


        APPEND l_r_addr_delv TO l_t_addr_delv.

        CLEAR l_r_items,l_r_addr_delv,l_r_items_x,l_r_sched,l_r_sched_x.
      ENDIF.
    ENDLOOP.

    LOOP AT l_t_limits ASSIGNING FIELD-SYMBOL(<l_r_limits>).
      l_r_limits-pckg_no <l_r_limits>-pckg_no.
      l_r_limits-limit <l_r_limits>-limit.
      l_r_limits-no_limit <l_r_limits>-no_limit.
      l_r_limits-exp_value <l_r_limits>-exp_value.
      l_r_limits-no_frlimit <l_r_limits>-no_frlimit.
      APPEND l_r_limits TO l_t_limits.
    ENDLOOP.

*create po if line item is not empty
    IF l_t_items[] IS NOT INITIAL.
      TRY.
          CALL FUNCTION 'BAPI_PO_CREATE1'
            EXPORTING
              poheader               l_r_header
              poheaderx              l_r_header_x
            TABLES
              return                 l_t_return2
              poitem                 l_t_items
              poitemx                l_t_items_x
              poaddrdelivery         l_t_addr_delv
              poschedule             l_t_sched
              poschedulex            l_t_sched_x
              poaccount              l_t_acc
              poaccountx             l_t_acc_x
              polimits               l_t_limits
            EXCEPTIONS
              illegal_parameter_type 1
              OTHERS                 2.
        CATCH cx_sy_dyn_call_illegal_type INTO DATA(lr_exception1).
          WRITE'Exception:'lr_exception1->get_text).
      ENDTRY.
      DATA(l_r_return2l_t_return2[ ].

      IF l_r_return2-type CA 'AWE'.
        SORT l_t_return2 BY type ASCENDING.
        LOOP AT l_t_return2 ASSIGNING FIELD-SYMBOL(<l_r_return2>).
          IF <l_r_return2>-type CA 'AWE'.
            CONCATENATE  e_f_msg <l_r_return2>-message INTO e_f_msg SEPARATED BY ','.
          ENDIF.
        ENDLOOP.
*       Roll back all data update if not succesful and raise concatenation of bapi message back to workflow
        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      ELSE.
*         Commit Database changes if successful
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait abap_true.
      ENDIF.

    ENDIF.
  ENDMETHOD.


--------------------------------Adding workflow------------------------------------

Step 1: Create an activity PO_CREATE.


            * Binding task<---> method.


            * Binding workflow <--> step.

Step 2: Final workflow.



Step 3: Check and activate the workflow.

----------------------------------Testing the workflow------------------------------

Step 1: Create a PR.

Step 2: Check the Release codes in PR Header.

Step 3: Approve the PR in SWIA (In this case 3 levels of approvers. I approved 3 times L1, L2, L3).

Step 4: Check the PO in PR item level screen(status).


********************************Thank You*****************************

Comments

Popular posts from this blog

New syntax for append- VALUE (new syntax 7.4+) in ABAP

Read statement new syntax in ABAP. (7.4+).

Concatenation new syntax( 7.4+ ).