May 11, 2008

Initiating Workflow from pl/sql

There are many ways to invoke workflow. Let’s see how we can start a workflow from pl/sql package.

Inputs needed: Obviously, validated workflow which is saved in database.
Itemkey: Get the itemkey for the workflow you want to initiate.
Process Name: Get internal name of the procedure in the workflow which you want to initiate.

Call following APIs in the given sequence.

1> wf_engine.createprocess
This API creates a new runtime instance of the workflow process, Pass the item type and item key for the workflow.

2> wf_engine.setitemuserkey
Use this API to mark the new runtime instance of the workflow process with an
end–user key

3> wf_engine.SetItemAttrText
Use this API to set values for the item type attributes defined for workflow process .E.g.: Email ID, Description defined in the workflow.

4> wf_engine.setitemowner
Use this API to set the value of owner (Of Workflow)

5> wf_core.context
In case of exception, it will help in locating source of an error.

6> wf_engine.startprocess
Use this API to invoke the workflow for the item type and item key specified.

All the APIs are explain in Oracle Workflow API Reference Guide.


---Sample Code ---

DECLARE

l_itemtype VARCHAR2(30) := 'XX_TEST';
l_itemkey VARCHAR2(300) := 'TEST';

BEGIN

Begin
wf_engine.createprocess(l_itemtype, l_itemkey, 'XX_MAIN_TEST');
Exception
when others then
dbms_output.put_line('Error in create process:' sqlerrm);
End;

BEGIN
wf_engine.setitemuserkey(itemtype => l_itemtype
,itemkey => l_itemkey
,userkey => 'USERKEY: ' l_itemkey);
EXCEPTION
when others then
dbms_output.put_line('Error in set userkey process:' sqlerrm);
END;

BEGIN
wf_engine.setitemowner(itemtype => l_itemtype
,itemkey => l_itemkey
,owner => 'SYSADMIN');
EXCEPTION
when others then
dbms_output.put_line('Error in set owner process:' sqlerrm);
END;

BEGIN
wf_engine.startprocess(l_itemtype, l_itemkey);
dbms_output.put_line('Process started');
commit;
EXCEPTION
when others then
dbms_output.put_line('Error in set owner process:' sqlerrm);
END;
END;
/


-- Download the code.
Get the Code

When to use pl/sql calls?
Pl/sql calls can be used for mass uploads (not conversion), interface with the 3rd
party systems. Also to initiate approvals and to send emails from pl/sql.

In next posts I will explain which are the other methods to call workflows, how to monitor them and how to troubleshoot workflow issues.
Please let me know your comments/feedbacks.

Thanks,
Soham Khot