Blog > Discover approval level in MM Flexible Workflow agent determination BAdI

Discover approval level in MM Flexible Workflow agent determination BAdI

Mateusz Adamus SAP Integration Consultant
icon__calendar 2020-11-05

In this article you will learn:

  • How to calculate the current approval level in agent determination BAdI for MM Flexible Workflow

Reading time: 5 minutes

Introduction

Flexible Workflow is a Fiori integrated solution for creation of document approval processes. Its main strength, ease of use, comes from little or zero need for programming interference. Basic scenarios can be created using configuration only. If you have a more complex situation, you can use one of the available BAdIs – for example the MMPUR_WORKFLOW_AGENTS_V2 BAdI, which allows for customer specific approval step agent determination logic.

Approval Step Agent Determination Options

Options for approval step agent determination

The issue of missing approval level

At the first glance, the BAdI seems like a perfect solution for implementation of a complex agent determination logic. It consists of a single method called GET_APPROVERS, which is populated with basic information about the document, for which the scenario was triggered.

Agent Determination BADI Parameters

GET_APPROVERS method parameters


  • As you can see, there is the BusinessObject parameter, which will tell you whether it’s a Purchase Order, Purchase Requisition or other document.
  • There are document and item numbers in the PurchasingDocument and PurchasingDocumentItem parameters respectively.
  • There is the WorkflowScenario, which will provide you with the ID of the scenario, for which the determination has been triggered.

More information about the BAdI and its parameters can be found in the SAP OSS note #2646400. In the same note you can also read that the information about the current step (level) is available in the BAdI from the 2008 release only and only for Purchase Requisition and Central Purchase Requisition documents.

Why is the level information so important?

To be able to correctly determine the agents for the approval step, you need to know for which level the BAdI was executed. In a static scenario, where all approval steps are triggered, you can achieve this by simply counting the previous approvers, contained in the PreviousApproverList parameter. However, in case of two or more non-obligatory approval steps, or an obligatory step after a non-obligatory one, the PreviousApproverList count will give incorrect results. Consider the example as shown in the image below:

Flexible Workflow Scenario Comparison

Comparison of two Flexible Workflow scenarios


Scenario B shows that the level calculated based on PreviousApproverList will be untrue (3 instead of 4). This may cause assignment of an incorrect agent and faulty workflow processing.

Approval level calculation

Fortunately, you can calculate the current approval level with the information that is available in the system during the BAdI execution.

This information consists of two parts:

  • XML with the details of Flexible Workflow and its flow so far – it contains information about the steps (ACTIVITY) that have been processed (COMPLETED) or skipped (SKIPPED) before the BAdI execution.

Flexible Workflow Process Flow XML

Flexible Workflow process flow XML

  • Workflow log – it contains information about the steps that were skipped just before the BAdI execution.

Skipped Steps in Workflow Log

Information about skipped steps in Workflow log


You can use PurchasingDocument and WorkflowScenario parameters to obtain the first part. Method GET_WORKFLOW_INSTANCES of class CL_SWF_FLEX_DEF_FACTORY will return a list of Flexible Workflow instances executed for the document. The last instance executed (the one with the highest WORKFLOW_ID) is what you should be looking for. You then need to parse its XML and count the steps.

The second part is more tricky. Theoretically, there is the standard function module SWW_WI_LOG_GET_BUFFER that you can use to read the Workflow log. However, this function removes messages of the same type that were triggered in the same second, which makes it useless in this case.

Workflow Log Buffer Function

Function to read Workflow log buffer


To avoid this, you can use a FIELD-SYMBOL and direct Workflow log buffer assignment, which will give you access to all messages stored in the buffer.

Below you can find the whole logic with the method interface:

Step Count Method Interface

The parameters of the approval level calculation method

Summary

Even though the current approval level isn’t always necessary for agent determination, there are cases where the determination would be flawed without it. Fortunately, it is possible to calculate it with the use of the Workflow log and its processing history.

 

Read also:

1. IDoc modification made easy with ABAP Object Oriented Programming

2. Programming is an art! Secrets of ABAP with Michael

Mateusz Adamus SAP Integration Consultant
Working with SAP environment since 2007. Involved in several international projects as an ABAP developer and programming team leader. Mateusz has broad experience in designing and implementing custom SAP solutions. Supporter of all solutions that allow to streamline work processes.

Contact us

If for any reason, the thought of reaching out to us has crossed your mind, then by all means do not hesitate to get in touch with us, as we are more than happy to put to rest all of your concerns.