I was reading a recent post by Dynamics team – they are talking about different ways to manipulate business process stages in Dynamics 365 there:
It’s a great article, and it has some interesting insights. What definitely caught my attention is that, it seems, we should be able to manipulate business process flows using the workflows now. Isn’t it what we always wanted to do? Imagine how you could verify some conditions in the workflow and move the process flow to the right stage depending on whether those conditions have been met or not. That would add quite a bit of automation to the whole process.
And, it seems, it’s really doable now. I did manage to make it work – just keep reading and you’ll find out how. However, keep in mind this whole article applies to the post fall 2016 versions of Dynamics 365.
First, here is what we need to know:
- For each business process flow there is an entity in Dynamics now. You can give it a name when creating a business process flow
- That entity, among other fields, has two lookup fields: a lookup to the main entity (the one for which this process flow is supposed to be running), and a lookup to the process stage entity
- Every time a process is activated, a new record of that entity type is created
- Active Stage lookup field in that record gets updated every time we move the process through the stages
To test how it works, I did set up a new entity and a new business process. Here is how the solution looks like:
- Process Demo entity is the main entity which will have a process etc
- Main Demo is the entity that was created for the Main Demo business process(there an entity per process, remember?)
- There is Main Demo process
- And there are a couple of workflows – I’ll explain what they are for down below
It turns out we can really use business process entity in the workflows just like we can do it with any other entity. However, there is a little problem there. Business process entity is related to the main entity as N:1 (many process instances, one main entity). Which means we can’t really access active business process from a workflow running on the main entity (it would be like asking a workflow running on the account record to access a particular contact linked to that account).
However, this relationship is, really, 1:1 rather than N:1. At any time, only one active process of that particular type can be associated with the main entity.
In other words, if we wanted to make it work, we would need to add a lookup from the main entity to the process entity. And that’s the first step – let’s add a lookup:
Once there is a lookup, we can use it in the workflow running for the main entity. Problem is, we still need to have some value in that lookup first.
That’s what one of those workflows is for:
BTW, do you see something unusual there? Where did that new trigger called “process is applied” come from? It seems to be one of the features of this new version of Dynamics, and it’s exactly what we need. This workflow will start whenever a process is applied to the main entity, and it will populate the lookup field on the main entity.
Once it’s done, we can use that lookup value in the workflows running for the main entity.
That’s what the second workflow is for:
The workflow will run when a new record is created or when a checkbox field is checked off. It will verify whether the checkbox is selected, and, if yes, it will modify active process stage.
And that’s, actually, it. I just got a workflow that can change active stage of the business process!
PS. Quite frankly, this was a little condensed version of how it all happened, so, here is what you’ll find in the next post:
- I’ll provide a link to the unmanaged solution file
- I’ll describe a few error messages I’ve run into while working on it