How do I trigger a workflow from a business rule?

Blair5
Tera Guru

We have an order guide and one of the items needs to wait for another item to complete. In the workflow for the first item, I am creating an event when the workflow completes. On the second workflow (the one that is waiting for the first) it is 'listening' for that event to queue. I've looked through the forums and see that to accomplish this, I need to write a business rule that would trigger the event on the second workflow.

I found this BR on another forum post:



fireWorkflowEvent();

function fireWorkflowEvent() {
var wf = new Workflow().getRunningFlows(current);
while(wf.next()) {
new Workflow().broadcastEvent(wf.sys_id, "b4d35f960a0a3cac011ac12fae09d514");
}
}


I've implemented this, however I'm not sure what I need to do for the workflow that is waiting. I currently have a "Wait for WF event" on it, but that isn't working. Does anyone know what I'm missing? Or - Can any one suggest a different way to approach this?

Thanks.

6 REPLIES 6

Chuck Tomasi
Tera Patron

Blair,

A better implementation of that function would be:



fireWorkflowEvent('my.event.name');

function fireWorkflowEvent(evt) {
var wf = new Workflow().getRunningFlows(current);
while(wf.next()) {
new Workflow().broadcastEvent(wf.sys_id, evt);
}
}


Now your workflow "Waitfor WF Event" can be waiting for "my.event.name" (assuming you've entered this in the event registry) and the business rule can trigger it.

If you use a global business rule or on-demand function (script include) you can call the fireWorkflowEvent() function from any script you like.


I cannot get this to work at all. My workflow will not continue when my i fire my event from a business rule. Ive put outputs to the log and it seems like the script include catches the event but it never makes it in to the while loop. and if i make a simple if statements using wf.hasNext() i get false?


Was not there a catch with the "current" variable inside the function fireWorkflowEvent(evt) ? This is quite dangerous as it is a global variable and we should not use that, better is to write it as following:



  1. fireWorkflowEvent(current,'my.event.name');  
  2.  
  3. function fireWorkflowEvent(recordGR, evt) {  
  4.     var wf = new Workflow().getRunningFlows(recordGR);  
  5.     while(wf.next()) {  
  6.           new Workflow().broadcastEvent(wf.sys_id, evt);  
  7.     }  
  8. }

We have Catalog Item, which can raise Request, RITM & Task. This is creating a single catalog task. while closing this Task, I have noticed that Item Designer - Fulfillment workflow's activity "Wait for WF Event" doesn't proceed any further. TASK get closed however its related RITM & REQ are still open. For closure of TASK, user is creating on customized Save button.



which has below line of code:-


action.setRedirectURL(current);


current.update();


if (! current.isActionAborted())


      action.setRedirectURL(current);



Could anyone of you have seen this issue before and let me know how to rectify the same? Do you think I should write the below code on UI action based in SC_TASK table to call that Workflow event explicitly through UI action.



var wf = new Workflow().getRunningFlows(current);


while(wf.next()) {


    new Workflow().broadcastEvent(wf.sys_id, 'sc_ic_req_task_complete');


}


Because if this event gets called then rest of the activities gets executed sequentially.