Getting the executing Workflow Context

TimbitGrrl
Kilo Contributor

I've been working at this for hours and hours now.

I have a workflow that creates an incident. I want to populate the Workflow Context field with the context that is currently executing.

On the first run, it will populate with the correct context.

On any subsequent runs, it will populate it with the first context that ran. The sys_id is that of the first context and not the executing context.

I've tried getting the workflow context in various ways. Here is my latest example where I tried checking to see if it was executing so I would get only the executing context:


var wfc = new Workflow().getContexts(current);

while(wfc.next()) {
if (wfc.state == "executing") {
task.u_workflow_context = wfc.sys_id;
}
}


I've also tried doing a getRunningFlows call.

I've tried using workflow.sys_id but that is for the version rather than the current executing workflow from the looks of it.

Any ideas of how I can get the currently executing context sys_id?

3 REPLIES 3

TimbitGrrl
Kilo Contributor

I've attempted to get the context in another manner by querying the wf_context table and taking the row with the state set as "executing"... It STILL gives me the original workflow_context and not the currently executing one, even though printing out the values I retrieved shows the state as "executing"...



var gr = new GlideRecord('wf_context');
gr.addQuery('id',current.sys_id);
gr.addQuery('state', 'executing');
gr.query();

if (gr.next()) {
task.u_workflow_context = gr.sys_id;

// Printing out these results gives the correct id (the record the workflow is executing against),
// the proper state of "executing" and the incorrect sys_id (the first workflow context sys_id which is
// not the current executing context.)
task.description = "id " + gr.id + ", state " + gr.state + ", sys id " + gr.sys_id;
}


Have you checked that the state value is actually 'executing' and not a numeric value?


bipworld
Tera Contributor

You may use following API: 

getRunningFlows(GlideRecord record)

Gets all the currently running workflow contexts for the input record.

The input record is any record on any table for which the caller wants the running workflow contexts.

 

//where current is a task record with a workflow context
      var wf = new Workflow().getRunningFlows(current);
      while(wf.next()) {     
          // Your code.
      }