SAP ABAP - Custom Agent Determination Supplier Invoice Flexible Workflow Approval

Approval Supplier Invoice document (MIR7) using flexible workflow with custom agent determination.

Using custom agent determination for supplier invoice approval with flexible workflow in SAP involves several steps. Below is a step-by-step guide to help you set this up:

Step 1: Understand the Requirement

  • In my case, custom agent determination need to maintain with custom table. Below the custom table needs:


  • After create both of tables, need to create custom CDS view which will be used in custom logic apps code

  • First CDS View ZInvoiceWf 
    @AbapCatalog.sqlViewName: 'ZIWFINVOICE'
    @AbapCatalog.compiler.compareFilter: true
    @AbapCatalog.preserveKey: true
    @AccessControl.authorizationCheck: #CHECK
    @ClientHandling.algorithm: #SESSION_VARIABLE
    @EndUserText.label: 'Supplier Invoice Workflow'
    @VDM.viewType: #COMPOSITE
    @VDM.lifecycle.contract.type: #SAP_INTERNAL_API

    @ObjectModel.usageType.sizeCategory: #M
    @ObjectModel.usageType.serviceQuality: #A
    @ObjectModel.usageType.dataClass: #MASTER
    @ObjectModel.supportedCapabilities:
      [  #CDS_MODELING_DATA_SOURCE,
         #CDS_MODELING_ASSOCIATION_TARGET,
         #SQL_DATA_SOURCE                  ]
    define view ZI_SUPP_INV_WF 
      as select from zmmt007 as ZInvoiceWf 
    {
        key ZInvoiceWf.function as Function,
        key ZInvoiceWf.fundesc as FuncDesc,
        key ZInvoiceWf.app_lvl as AppLvl,
        ZInvoiceWf.bus_usr as BusUser    
    }

    Second CDS View ZI_SUPP_INV_REL_MAP
    @AbapCatalog.sqlViewName: 'ZIWFINVOICEREL'
    @AbapCatalog.compiler.compareFilter: true
    @AbapCatalog.preserveKey: true
    @AccessControl.authorizationCheck: #NOT_REQUIRED
    @EndUserText.label: 'Release Invoice Mapping'
    @VDM.viewType: #COMPOSITE
    @VDM.lifecycle.contract.type: #SAP_INTERNAL_API

    @ObjectModel.usageType.sizeCategory: #M
    @ObjectModel.usageType.serviceQuality: #A
    @ObjectModel.usageType.dataClass: #MASTER
    @ObjectModel.supportedCapabilities:
      [  #CDS_MODELING_DATA_SOURCE,
         #CDS_MODELING_ASSOCIATION_TARGET,
         #SQL_DATA_SOURCE                  ]
    define view ZI_SUPP_INV_REL_MAP as select from zmmt008 as ZInvoiceRel 
    {
        key ZInvoiceRel.zrel_group,
        key ZInvoiceRel.zrel_strat,
        key ZInvoiceRel.zfunction,
        key ZInvoiceRel.zsign,
        key ZInvoiceRel.zcurr,
        zamount

    @AbapCatalog.sqlViewName: 'ZVRBCO'
    @AbapCatalog.compiler.compareFilter: true
    @AbapCatalog.preserveKey: true
    @AccessControl.authorizationCheck: #NOT_REQUIRED
    @EndUserText.label: 'Invoice Account Assignment'
    define view ZI_INV_RBCO as select from rbco {
        key belnr,
        key gjahr,
        key buzei,
        key cobl_nr,
        wrbtr,
        sgtxt,
        txjcd,
        kostl
    }

    after create all CDS view need to publish all cds view to whitelisted object, to publish cds view


    Step 2: Create a Custom BADI Implementation

Create BADI implementation from sap fiori Custom Logic Apps:

Create new custom logic


After create, will show the custom logic page, in custom logic page go to Filter tab and add new filter workflowscenario = WS00800303 


add this code to custom logic code
    DATA:
      ls_badi_approver     TYPE if_mrm_workflow_agents=>bd_mmpur_s_badi_approver,
      lt_badi_approver     TYPE if_mrm_workflow_agents=>bd_mmpur_t_badi_approver,
      ls_previous_approver TYPE if_mrm_workflow_agents=>bd_mmpur_s_previous_approver,
      ls_new_approver      TYPE if_mrm_workflow_agents=>bd_mmpur_s_badi_approver,
      lv_count             TYPE i.

    DATA:
     lt_wf type table of ZI_SUPP_INV_WF,
     lt_wf_rel TYPE TABLE OF zi_supp_inv_rel_map,
     lt_wf_rel2 TYPE TABLE OF zi_supp_inv_rel_map,
     ls_wf_relx TYPE  zi_supp_inv_rel_map,
     ls_wf_rel2 TYPE  zi_supp_inv_rel_map,

     lv_app_level TYPE I.


    lv_app_level = stepinfo-current_step.

    SELECT *
        from I_SupplierInvoiceAPI01 WITH PRIVILEGED ACCESS
        where SupplierInvoice = @supplierinvoice and
              FiscalYear = @FiscalYear
        INTO TABLE @DATA(lt_SupplierInv).

    SELECT *
        from ZI_INV_RBCO WITH PRIVILEGED ACCESS
        where belnr = @supplierinvoice and
              gjahr = @FiscalYear
        INTO TABLE @DATA(lt_supplier_inv_assignment).

    READ TABLE lt_SupplierInv INTO DATA(ls_invoice) INDEX 1.
    READ TABLE lt_supplier_inv_assignment INTO DATA(ls_assignement) INDEX 1.

    IF ls_invoice-companycode = '0001' AND
       ls_invoice-ACCOUNTINGDOCUMENTTYPE = 'RE' AND
       ls_invoice-INVOICEGROSSAMOUNT >= 1.

        SELECT * FROM zi_supp_inv_rel_map WITH PRIVILEGED ACCESS
            WHERE zrel_group = 'WPO-GEN-WF' AND
                  zrel_strat = 'ALL'
        INTO TABLE @lt_wf_rel.

    ELSEIF ls_invoice-companycode = '0001' AND
           ls_invoice-ACCOUNTINGDOCUMENTTYPE <> 'RE'.
*           AND
*           ls_invoice-INVOICEGROSSAMOUNT <= 100000.

        SELECT * FROM zi_supp_inv_rel_map WITH PRIVILEGED ACCESS
            WHERE zcurr = @ls_invoice-documentcurrency AND
                  zrel_strat = @ls_assignement-kostl
        INTO TABLE @lt_wf_rel2.

        LOOP AT lt_wf_rel2 ASSIGNING FIELD-SYMBOL(<fs_wf_rel>).
            IF ls_invoice-INVOICEGROSSAMOUNT > <fs_wf_rel>-zamount AND <fs_wf_rel>-zsign = 'GT'.
                ls_wf_relx = <fs_wf_rel>.
                EXIT.
            ELSEIF ls_invoice-INVOICEGROSSAMOUNT >= <fs_wf_rel>-zamount AND <fs_wf_rel>-zsign = 'GE'.
                ls_wf_relx = <fs_wf_rel>.
                EXIT.
            ELSEIF ls_invoice-INVOICEGROSSAMOUNT < <fs_wf_rel>-zamount AND <fs_wf_rel>-zsign = 'LT'.
                ls_wf_relx = <fs_wf_rel>.
                EXIT.
            ELSEIF ls_invoice-INVOICEGROSSAMOUNT <= <fs_wf_rel>-zamount AND <fs_wf_rel>-zsign = 'LE'.
                ls_wf_relx = <fs_wf_rel>.
                EXIT.
            ELSEIF ls_invoice-INVOICEGROSSAMOUNT = <fs_wf_rel>-zamount AND <fs_wf_rel>-zsign = 'EQ'.
                ls_wf_relx = <fs_wf_rel>.
                EXIT.
            ENDIF.
        ENDLOOP.

        SELECT * FROM zi_supp_inv_rel_map WITH PRIVILEGED ACCESS
        WHERE zrel_group = @ls_wf_relx-zrel_group AND
              zrel_strat = @ls_assignement-kostl
        INTO TABLE @lt_wf_rel.

    ENDIF.

    IF lt_wf_rel is not initial.
        select * FROM ZI_SUPP_INV_WF WITH PRIVILEGED ACCESS
            FOR ALL ENTRIES IN @lt_wf_rel
            WHERE function = @lt_wf_rel-zfunction
                AND APPLVL   = @lv_app_level
                into TABLE @lt_wf.

        sort lt_wf by AppLvl.
    ENDIF.

    clear lv_count.
    loop at lt_wf into data(ls_wf).
        clear ls_badi_approver.
        ls_badi_approver-businessuser = ls_wf-BusUser.
        ls_badi_approver-approvallevel = ls_wf-AppLvl.
        APPEND ls_badi_approver TO lt_badi_approver.
        clear ls_wf.
        lv_count = lv_count + 1.
    endloop.

*stepinfo-total_steps = lv_count.

**  determine the next approval level and appropriate approvers
    READ TABLE lt_badi_approver INTO ls_badi_approver INDEX 1.
    LOOP AT lt_badi_approver INTO ls_new_approver.
*                WHERE approvallevel = ls_badi_approver-approvallevel.
      APPEND ls_new_approver-businessuser TO approverlist.
    ENDLOOP.

**  remove the previous approvers from the list of BAdI approvers
    LOOP AT previousapproverlist INTO ls_previous_approver.
      READ TABLE lt_badi_approver INTO ls_badi_approver
                              WITH KEY businessuser = ls_previous_approver-businessuser.
      CHECK sy-subrc = 0.
      DELETE lt_badi_approver WHERE approvallevel = ls_badi_approver-approvallevel.
    ENDLOOP.

    


Save the code and publish the custom logic code.





Define Flexible workflow for supplier invoice 











Comments

Popular posts from this blog

PHP MySql CRUD Dengan Konsep MVC

Laravel - Sistem Informasi Perpustakaan Sederhana Laravel

Cara Sederhana Multi Insert Data Dengan PHP - MySQL