Wednesday, June 10, 2009

Code sample to run workflow with API

Symptom: how to create and handle workflow instance, when a workflow
template (process) is defined by graphical Workflow Manager in
Desktop Client.

Solution:

1. Create Workflow definition (dm_process) with Desktop.
Initiate --> AutoFirst --> ManualSecond --> End
AutoFirst : Automatic Activity run with dm_noop_auto_method.
ManualSecond : Manual Activity run by a single user.
Package from Initiate to AutoFirst, optional package Package0
is defined.Package from AutoFirst to ManualSecond, optional
package Package0 is defined.

So this workflow can be started with no package added. If the
package is set to CURRENT, then this workflow can be started
only with attached package.

2. Get r_object_id of this Workflow definition (Process)
DQL> select r_object_id, object_name from dm_process

3. Create a workflow instance
API> create,c,dm_workflow

4d00800080000911

API> set,c,l,process_id
SET> 4b0080008000392a
OK
API> set,c,l,object_name
SET> mySimpleWF
OK
API> save,c,l
OK

You can see a Workflow instance is created.
DQL> select * from dm_workflow

r_object_id object_name process_id
------------------- --------------- -------------------
4d00800080000911 mySimpleWF 4b0080008000392a

4. Start the workflow
API> execute,c,4d00800080000911

But there is no dmi_workitem or dmi_package.
DQL> select r_object_id, r_act_seqno, r_runtime_state from dmi_workitem
r_object_id r_act_seqno r_runtime_state
---------------- ---------------------- ----------------------
(0 rows affected)
DQL> select r_object_id, r_workflow_id, r_package_name from dmi_package
r_object_id r_workflow_id r_package_name
--------------- ---------------- ----------------
(0 rows affected)
5. Add package to the first activity of workflow
API> addpackage,c,4d00800080000911,AutoFirst,Input:0,Package0,dm_sysobject,,,090080008000193f
...
4900800080000924
Where
AutoFirst : Activity name in Workflow instance
Input:0 : Input port name for the first activity

Package0 : Package name

090080008000193f : r_object_id of document object which shall be delivered to next activity.

4900800080000924 is the r_object_id of the dmi_package object, just created. And dmi_workitem and dmi_package objects are created, according to the workflow definition.

I watched that dmi_workitem objects are created only after all the dmi_package objects are attached for the first activity.

DQL> select r_object_id, r_act_seqno, r_runtime_state, r_auto_method_id from dmi_workitem

r_object_id r_act_seqno r_runtime_state r_auto_method_id
---------------- ---------------------- ---------------------- ----------------
4a0080008000091f 1 0 0000000000000000
4a0080008000091e 0 2 1000800080003528
DQL> select r_object_id, r_workflow_id, r_package_name from dmi_package
r_object_id r_workflow_id r_package_name
---------------- ---------------- ----------------
4900800080000925 4d00800080000911 Package0

4900800080000924 4d00800080000911 Package0

The package name should be defined in workflow definition, otherwise
addpackage API fails.

API> addpackage,c,l,AutoFirst,Input:0,Package1,dm_sysobject,,,090080008000193f

[DM_WORKFLOW_E_INVALID_PACKAGE_INPUT_PORT]error: "The package, Package1, is not in the specified input port, Input:0."

6. Acquire the workitem for corresponding to the second activity of the workflow

API> acquire,c,4a0080008000091f
DQL> select r_object_id, r_act_seqno, r_runtime_state, r_auto_method_id from dmi_workitem

r_object_id r_act_seqno r_runtime_state r_auto_method_id
---------------- ---------------------- ---------------------- ----------------
4a0080008000091f 1 1 0000000000000000
4a0080008000091e 0 2 1000800080003528

7. Acquire the workitem for corresponding to the second activity of the workflow
API> complete,c,4a0080008000091f
...
OK
Then the workflow, workitem and package objects are removed, since this is the next activity is End activity.

DQL> select * from dm_workflow
r_object_id object_name process_id
------------------- --------------- -------------------
(0 rows affected)

DQL> select r_object_id, r_act_seqno, r_runtime_state from dmi_workitem
r_object_id r_act_seqno r_runtime_state
--------------- ---------------------- ----------------------
(0 rows affected)

DQL> select r_object_id, r_workflow_id, r_package_name from dmi_package
r_object_id r_workflow_id r_package_name
---------------- ---------------- ----------------
(0 rows affected)

No comments: