Workflows tend to be part of the core business in SAP Sourcing and business requirements will most often transform a 'simple' workflow process into a very complex one.
Since the introduction of WF in Sourcing (and the TWE 'era') a specific pattern to script workflows has emerged and it has been carried forward into all projects. Today we are going to change that.
**WARNINGS**
- Implementing recommendations in this blog post might make your life easier
- Throughout this blog post it will be assumed that consultants have following knowledge
- Workflows definition / update / scripting
- Java EE/BeanShell
WORKFLOW TYPOLOGY
The typology used throughout projects is fairly similar:
- Workflow definition with X number of Approval Steps (total maximum possible steps)
- Approval Matrix Definition maintained *somewhere*
- Specific Approval Sequence picked up in PRE-PHASE-CHANGE script and added to the document into an Extension Collection
- Workflow scripting handles the 'next approver' from the document Extension Collection
So, WHAT IS THE PROBLEM?
For each approval step the script is required to
- Check if approval status is APPROVED or REJECTED
- APPROVED
- Approval Sequence is complete -> move document to 'Approved' phase
- If not complete, add next approver
- REJECTED
- move document to 'Draft' (or previous phase)
Pattern in case of APPROVED for above is:
if(approvalMatrix.size() == 1) {
//move to Approved
}
if(approvalMatrix.size() >= 1) {
//add next approver
}
if(approvalMatrix.size() == 2) {
//move to Approved
}
if(approvalMatrix.size() >= 2) {
//add next approver
}
WHY IS THAT A PROBLEM? And why is it complicating our lives?
The pattern above implies that:
- there is one distinct version of PRESCRIPT for EACH workflow step
- similarly, there will be one distinct version of POSTSCRIPT for EACH workflow step
To maintain such a workflow, an IT consultant will have to:
- save/maintain each prescript/postscript version individually
- do a lot of repetitive work for any minor update
- be VERY careful when doing updates not to disturb the process
WHAT IS THE SOLUTION?
The solution is to make scripting GENERIC.
- Single version of PRESCRIPT for ALL steps
- the script should be able to 'know' from which step it was executed
- Single version of POSTSCRIPT for ALL steps
- the script should be able to 'know' from which step it was executed
HOW?
- Step 1: Make sure you have a gate identifier. Solution is to 'decode' the current step from the Activity ID (last digit/digits)

- Step 2: Use following code to identify gate -> 'current approval step'
- Script will get ending digit(s) from identifier:
- 'approval_gate_1' = 1
- 'approval_gate_12' = 12
- 'approval_gate_1485' = 1485 :grin:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
private Integer gate = null;
// btoma - Get the step No. from gate NativeID - last digit(s)
// btoma - to facilitate PRESCRIPT on all steps
void getGate() {
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(nativeName);
while(m.find()) {
gate = Integer.valueOf(m.group());
}
}
getGate();
- Step 3: Update WF script code, to use gate instead of hardcoded numbers
if(approvalMatrix.size() == gate) {
//move to Approved
}
if(approvalMatrix.size() >= gate) {
//add next approver
}
- Step 4: Copy-Paste your PRESCRIPT on rest of WF steps
- Step 5: Repeat 1-4 for POSTSCRIPT
OTHER RECOMMENDATIONS?
- Maintain scripts locally, in a source control sistem (SVN, GIT).
- Perform changes locally, and copy-paste your script to all WF steps in one go.
- Don't forget about logging and error reporting
The end.
Bogdan Toma