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#.

RetrieveProcessInstancesRequest:

You need to retrieve the current active BPF instance and remove this, you simply use the code as follows:

RetrieveProcessInstancesRequest req = new RetrieveProcessInstancesRequest()
{
       EntityId = target.Id,
       EntityLogicalName = target.LogicalName
};
RetrieveProcessInstancesResponse resp = (RetrieveProcessInstancesResponse)sdk.Execute(req);

if (resp != null && resp.Processes != null && resp.Processes.Entities.Count > 0)
{
       Entity currentProcess = resp.Processes.Entities[0]; // first one is always the active process
}


BusinessProcessFlowInstance entity:

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.

image

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.

string processName = currentProcess.Get<string>("Process.name");

QueryExpression qe = new QueryExpression("workflow");
qe.Criteria.AddCondition("name", ConditionOperator.Equal, processName);
qe.NoLock = true;
qe.TopCount = 1;
qe.ColumnSet.AddColumn("uniquename");
EntityCollection bpfs = sdk.RetrieveMultiple(qe);

string bpfUniqueName = bpfs.Entities[0].Get<string>("uniquename");


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:

sdk.Delete(bpfUniqueName, currentProcess.Id);


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:

SetProcessRequest request = new SetProcessRequest();
request.Target = new EntityReference(target.LogicalName, target.Id);
request.NewProcess = new EntityReference("workflow", workflowId);
sdk.Execute(request);


Summary:

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.