In all the years I have used SAP_WAPI_WORKITEMS_TO_OBJECT, the use of the ‘Selection Status Variant’ has been the most confusing to me. I’ve used it roughly 20 times, and each time, I seem to forget which values will get what results, so I thought I would try to sort it out for other people here.
You may be using SAP_WAPI_WORKITEMS_TO_OBJECT to find any other workflows that have been instantiated for an object/key pair, or <gasp> for some sort of reporting (contrary to Paul Bakker’s excellent advice)
This particular WAPI (and I am going to stop typing the whole name now) is the preferred method of locating workflows related to specific objects, replacing the function SWI_WORKITEMS_OF_OBJECT_GET. If you are still using that function – or some <ahem> custom function like it – you would be well-advised to change over to SAP_WAPIs – and not just for this purpose, but there is a whole range of SAP_WAPIs available to you, so get on the bus, OK?
First off, I can use transaction SWI6 to find all the workflows that have worked on an individual object. This is a good start towards testing SAP_WAPI, as I trust SWI6. We can see that there are 12 workflows that have started on this object – in a variety of states.
Running SAP_WAPI with no task_filter and merely specifying the object type and key, defaulting in SELECTION_STATUS_VARIANT ‘0001’ yields these results.
In the return structure WORKLIST, we can see 3 entries.
It doesn’t take a rocket scientist to determine that these three workflow IDs are the same ones that are listed as ‘in process’ from Diagram 1. So, selection status variant 0001 must mean ‘All workflows that are In Process’. Fair enough.
Using Selection Status Variant ‘0002’ returns 6 results in WORKLIST:
OK, so Variant 0002 must mean – maybe, give me the completed workitems?
Kind of like ‘Bring out yer dead’ from Monty Python and the Holy Grail?
Diagram 5 – courtesy of Tumblr
Interesting, Variant 0003 returns 6 results, which corresponds to the other 6 workitem ids, which are *not* completed.
Diagram 6 – Workitem IDs IN PROCESS
Variant 0004 returns all 12 workitem IDs. I went ahead and tried running with additional status variants but 0004 is the most comprehensive and you don’t get any different results just because you threw a ‘9999’ in the status variant. But hey, you can’t blame me for trying.
Diagram 7 – All Workitems
BUT, remove that little ‘X’ in Top_Level_Items, and run with variant ‘0001’ again? You will get a list of all the workitems – for the 3 that were in process – you will get the workitem IDs for all Workflows (also Subworkflows) Event types, and Dialog Tasks.
Again, without Top_Level_Items selected, variant ‘0002’ returns all tasks – dialog, event, and background for the Completed or cancelled workflows.
Try using the Task Filter – I entered the task ID of an approval task used in this particular workflow.
Variant 1 returns 0 items, as no approval tasks were ‘in process’.
Variant 2 returned all the approval tasks that had been completed.
Variant 3 returned any approval tasks that were ‘READY’.
Variant 4 returned all the approval tasks, ready or not.
So, as any goodprogrammer will tell you, we always need to be conscious of the runtime even when using our best (ie: SAP Delivered J) functions. Now, I am no ABAP Detective, but I ran a trace on this function, using Variant 4 (gimme all you got) and also using the Task Filter.
The results? Well, they are not surprising at all.
Diagram 8 – SQL and RFC trace using Top-Level Workitems
Diagram 9 – Trace using Task Filter
So I could have been greedy (and lazy) and just run SAP_WAPI_blabbity-blah with the highest variant and no filter, but eventually my bad deeds would catch up to me (they always do). Sooner or later, that difference of 15,000 durtns (what is a durtn? Heck it’s a unit of measure, and in this case, unlike with your bank balance, it’s always good to see it go down) was going to come back to haunt me. Passing in the smallest – most significant – piece of data that I needed to get returned will not only simplify my programming in the caller, but it ensures that the Basis team does not come down on me. Or that the end users don’t wander away from their desks for a coffee break.
I hope I have demystified at least one of the SAP_WAPI functions. Once you get used to them, they are quite good. You will find SAP_WAPIs to do everything from raise an event (a much preferred method of starting workflows than simply starting the workflow) to container manipulation to substitution management.
When you DO use one, I hope you will share what it’s for, and expose any little variants which could help others here.