Thursday, July 17, 2008

Workflow Business Events API's Part 2

In Part 1, I discussed how to get the information for Business Events using the Workflow Business Event API's. Here I will discuss how to create Business Events and Subscriptions using the same packages:

1) wf_events_pkg
2) wf_event_subscriptions_pkg

To add a new event from scratch, the wf_events_pkg.receive procedure can be used. Here is an example of creating a new custom event:

DECLARE
l_xml_txt varchar2(4000);
l_evt_name varchar2(100) := 'oracle.apps.xxcust.event.NewEvent';
l_evt_desc varchar2(255) := 'Custom event created by apps';
BEGIN
l_xml_txt := '<WF_TABLE_DATA>
<WF_EVENTS>
<VERSION>1.0</VERSION>
<GUID>#NEW</GUID>
<NAME>'||l_evt_name||'</NAME>
<TYPE>EVENT</TYPE>
<STATUS>ENABLED</STATUS>
<GENERATE_FUNCTION/>
<OWNER_NAME>APPS</OWNER_NAME>
<OWNER_TAG>APPS</OWNER_TAG>
<CUSTOMIZATION_LEVEL>U</CUSTOMIZATION_LEVEL>
<LICENSED_FLAG>Y</LICENSED_FLAG>
<JAVA_GENERATE_FUNC/>
<DISPLAY_NAME>'||l_evt_name||'</DISPLAY_NAME>
<DESCRIPTION>'||l_evt_desc||'</DESCRIPTION>
</WF_EVENTS>
</WF_TABLE_DATA>';
-- Create the event
WF_EVENTS_PKG.RECEIVE(l_xml_txt);
DBMS_OUTPUT.PUT_LINE('Event: '||l_evt_name||' created');
COMMIT;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('ERROR CODE: '||SQLCODE);
DBMS_OUTPUT.PUT_LINE('ERROR MESSAGE: '||SQLERRM);
ROLLBACK;
END;

I would like to point out the "#NEW" token in the GUID tag. This indicates to the package that this is a new event and it needs to generate the GUID for the event. The GUID is the global unique id for the event and it is a RAW datatype. After successfully generating the event, we can query the GUID:

SELECT guid
FROM wf_events
WHERE name = 'oracle.apps.xxcust.event.NewEvent';

We will need the GUID when we add a subscription to the event. To add a subscription that calls a PL/SQL procedure, use the following block:

DECLARE
l_xml VARCHAR2 (4000);
l_evt_guid RAW (16) := '523DA1719EE6381EE0440003BAAFAFC6';
l_rule_func VARCHAR2 (100) := 'show_event_parameters.process_event';
-- Custom PL/SQL function
l_sub_guid RAW (16);
BEGIN
l_xml :=
'
<WF_TABLE_DATA>
<WF_EVENT_SUBSCRIPTIONS>
<VERSION>1.0</VERSION>
<GUID>#NEW</GUID>
<SYSTEM_GUID>#LOCAL</SYSTEM_GUID>
<SOURCE_TYPE>LOCAL</SOURCE_TYPE>
<SOURCE_AGENT_GUID/>
<EVENT_FILTER_GUID>'|| l_evt_guid||'</EVENT_FILTER_GUID>
<PHASE>50</PHASE>
<STATUS>ENABLED</STATUS>
<RULE_DATA>KEY</RULE_DATA>
<OUT_AGENT_GUID/>
<TO_AGENT_GUID/>
<PRIORITY>120</PRIORITY>
<RULE_FUNCTION>'|| l_rule_func||'</RULE_FUNCTION>
<JAVA_RULE_FUNC/>
<STANDARD_TYPE/>
<STANDARD_CODE/>
<ON_ERROR_CODE>ABORT</ON_ERROR_CODE>
<ACTION_CODE>CUSTOM_RG</ACTION_CODE>
<WF_PROCESS_TYPE/>
<WF_PROCESS_NAME/>
<PARAMETERS/>
<OWNER_NAME>fnd</OWNER_NAME>
<OWNER_TAG>fnd</OWNER_TAG>
<CUSTOMIZATION_LEVEL>U</CUSTOMIZATION_LEVEL>
<LICENSED_FLAG>Y</LICENSED_FLAG>
<DESCRIPTION/>
<EXPRESSION/>
</WF_EVENT_SUBSCRIPTIONS>
</WF_TABLE_DATA>';
wf_event_subscriptions_pkg.receive (x_message => l_xml);
COMMIT;
SELECT guid
INTO l_sub_guid
FROM wf_event_subscriptions
WHERE event_filter_guid = l_evt_guid;
DBMS_OUTPUT.put_line ('Subscription created GUID: '||l_sub_guid);
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ('ERROR CODE: ' || SQLCODE);
DBMS_OUTPUT.put_line ('ERROR MESSAGE: ' || SQLERRM);
ROLLBACK;
END;

So now we have created an event and added a subscription to that event using the Workflow Business Events API's. You can create any number of subscriptions with all of the same options that you have from the gui.

There is another method that can be used to extract and load events and subscriptions using FNDXLOAD, but that is a topic for another post.