How to Dynamically Switch Business Process Flow in Dynamics 365
Adam Murchison, 16 June 2020
Previously I had written about how to switch business process flow in Dynamics 365 here. This has the fundamentals of how to switch a BPF but did not touch on deleting the previously set BPF. You must delete the previously set BPF for there to be only one active BPF, users may see the old one if this is not deleted. Note this will require knowledge in C#.
You need to retrieve the current active BPF instance and remove this, you simply use the code as follows:
The variable ‘currentProcess’ will contain the current active business process flow instance, with logical name "businessprocessflowinstance". You may believe that you can delete this record and then the target entity record won’t have an active business process flow but you’d be wrong. You cannot delete a “businessprocessflowinstance” record, instead you have to query the schema name of your business process flow by the ‘Process.name’ which is an attribute on the “businessprocessflowinstance” record or the currentProcess variable. Below is a list of the attributes retrieved on the “businessprocessflowinstance” record.
Querying the workflow schema name:
To do this you simply query the workflow entity, where the name is equal to the Process.name attribute from the businessprocessflowinstance.
This will return the active business process flow; you can now get the ‘uniquename’ which is the schema name of the business process flow so that you can delete the active business process flow on your record as shown below.
Delete active business process flow instance and set new active:
Now that you’ve deleted the current active business process flow, you can set your desired business process flow. Just like the other blog you do this as follows:
When switching business process flows there was not enough information online about retrieving the schema name of the current active business process flow so that you can delete the current instance. I hope this blog provides all the information and code for you to be able to dynamically switch business process flows to suit your needs.