Launching Workflow from a Trigger
There are certain situations when you may need to launch a workflow from a trigger, which is supported but in a restricted manner. Whenever a workflow activity starts, a savepoint is issued – which is not allowed in a trigger, so if you need to launch your flow (or restart it) from a trigger, you need to ensure that the code doesn’t get as far as issuing a savepoint by deferring it to the background.
In order to determine whether the activity should run in the foreground or the background, the workflow engine checks an externalized constant called WF_ENGINE.THRESHOLD. If the cost of the activity in the workflow is above the threshold, then the activity is deferred to the background and you need to run a background engine to execute it.
In order to force the engine to defer the thread, you need to set the threshold to a value which is going to be lower than the cost of the activity, and then communicate witht= the workflow engine. Once you have called the engine, you MUST ensure that you set the threshold back, or all workflow activities from that point on will defer to the background.
Once you’ve deferred the thread, run a background engine (WF_ENGINE.BACKGROUND or there is a seeded concurrent program in eBS) to start the process moving again. Here’s the code to do it:
begin
save_threshold := wf_engine.threshold;
wf_engine.threshold := -1;
wf_engine.createprocess('<ItemType>','<ItemKey>','<Process>');
wf_engine.startprocess('<ItemType>','<ItemKey>');
wf_engine.threshold := save_threshold;
exception
when others then
-- remember to reset the threshold
if wf_engine.threshold <= 0 then
wf_engine.threshold := save_threshold;
end if;
-- continue with normal error handling
end;
This works fine if you are using the CreateProcess and StartProcess (or LaunchProcess) APIs, but doesn’t work for raising a business event. The next post will look at the only way that you can raise a business event from a trigger.



Leave a Reply