Prevent Incomplete Stages of a Business Process Flow in Dynamics 365

John Towgood, 21 November 2018

In Dynamics 365 every Business Process Flow has a corresponding entity which is automatically created and used to record state. This entity can be made available in grids, views, charts, and dashboards. Here is an example of the entity created for the Lead To Opportunity Business Process Flow called Lead To Opportunity Sales Process, take note that you cannot open a record like standard entities.

image

Since the Business Process Flow state is not stored against a record like previous versions of Dynamics 365 but stored against the Business Process Flow entity record, we would need to query this entity to identify what state the current Business Process Flow is on for a specific record.

Let’s take a scenario where users are required to complete all stages for a Lead to Opportunity Business Process Flow before the Opportunity can be closed as won.

1. Create a plugin that checks if Opportunity Close is the target entity
2. Obtain the opportunityid field to get the Id of the Opportunity record being closed as won.

image

3. Query the Lead To Opportunity Sales Process entity to check a least 1 record has the opportunityid and a statuscode that is not Finished(2).
4. If there is a record found, then throw a plugin exception to stop the close as won process and display an error to the user indicating that the Opportunity can not be closed as won.

image

5. Register the plugin against the Win Message and the Opportunity Entity

image

Once the plugin is setup users will be presented with a Business Process Error when trying to close an Opportunity as won with incomplete stages.

image

Only when all stages have been Finished would the Opportunity be closed as won.

image

So, to conclude a business process flow state is stored against its corresponding entity and the status of Finished indicates that all stages have been completed for a specific record.