How can we tell in SQL which are the valid next workflow transitions?

1 minute read time.

This question was asked in a training class I taught recently.

Imagine the standard Opportunity workflow.

At any state within the workflow that are a number of further workflow rules available. These are the possible transitions to the next state within the workflow.

You can see that when the opportunity is in the Lead state there are a number of possible rules than can be called, and these are then available to the user.

But how could we find this out in our own programs?

To answer this we need to think about the data model for Workflow.

This means that we can find out the available workflow steps using the context of the record being workflowed.

If we know the ID of the record then everything else will come from that.

Consider an Opportunity Record. This opportuinity has the unique Id '11'. e.g. oppo_opportunityid = 11.

We can then find out the workflowinstance details.

e.g.

select oppo_workflowid from opportunity where oppo_opportunityid =11

This would provide the oppo_workflowid e.g. 1167

We can then retrieve the workflowinstance record.

select wkin_workflowid, wkin_currentstateid from workflowinstance where wkin_instanceid = 1167

e.g.

wkin_workflowid =2
wkin_currentstateid =10

This would provide the details of the workflowid, and the current state.

This can then be used to find the next possible transitions.

select wktr_nextstateid from workflowtransition where wktr_workflowid = 2 and wktr_stateid = 10