Get record and business process flow information in Power Automate flow step of Business process flow in Dynamics 365/ CDS

Are you having any of the following requirements?

  • You need to execute a Power Automate flow from Business process flow step.
  • You have configured a Power Automate flow to execute as Business process flow step and want to access the record which is displaying the business process flow.
  • You want to work with the Business process flow instance and take decisions based on the stage the record is currently in.

If you are having this requirement, you are not alone. Since the time Microsoft have released the capability to execute a flow as a Business process flow step in Dynamics 365/ CDS, customers and consultants alike are exploring the various possibilities on how this feature can be leveraged. And more often that not you require to access either the BPF instance or the record data from which the flow is currently executing.

While there are quite a few examples on how to execute a flow step from Business process flow, we need to understand how the flow will have access to the record and BPF instance. So let’s get started.

For this demo, I will create a simple leave request flow that the requestor will click to send an email to the approver with the leave request information.

The first and foremost thing that you should remember here is – “Any flow you intend to use as a flow step in Business process flow must be created within a solution. Otherwise the flow won’t be available for selection when you try to add the flow in the Business process step.

Select Common Data Service connector and then select – “When a record is selected” trigger. Observe I haven’t used the Common data service (current environment) connector as it does not have the trigger we are looking for.

When a record is selected -  Power Automate.

This is probably the most important step. I come across lots of examples which uses the “Manually run a flow trigger”. The whole agenda here is to pass the Business process flow and the record information to the flow. And for that it is essential that you use this trigger.

You might be wondering, when does this flow fire? What record should I select to trigger the flow? Whenever you are executing the Power Automate flow from Business process flow, the flow shall trigger. And the record context will be the Business process flow instance. Hence I configure the Power Automate flow as below. “Leave approval process flow” is my business process flow entity.

BPF - Flow step.

The next step is to fetch the Leave request record. For that I use the Common data service (current environment) connector and use the GetRecord action. But this is where it may get bit challenging. While we expect the output from the previous step as an entity of type “Leave approval process flow”, the final content is basically generic business process flow entity schema. If you run the flow you will see the below output.

"body": {
        "entity": {
            "entity": {
                "FlowsWorkflowLogId": "0299a4cf-56ab-4349-b6ea-00f4514a385d",
                "BPFInstanceId": "6d65e7cc-39fe-ea11-a815-002248049354",
                "BPFInstanceEntityName": "contoso_leaveapprovalprocessflow",
                "BPFDefinitionId": "1a19ef4d-a685-4676-8a6a-f2d988c3a280",
                "BPFDefinitionEntityName": "workflow",
                "StepId": "0492a8a7-6776-41ee-bfdf-46ad7be3e796",
                "BPFDefinitionName": "Leave Approval Process Flow",
                "BPFInstanceName": "Leave Approval Process Flow",
                "BPFFlowStageLocalizedName": "Apply",
                "BPFFlowStageEntityName": "contoso_leaverequest",
                "BPFFlowStageEntityCollectionName": "contoso_leaverequests",
                "BPFFlowStageEntityRecordId": "7719d0bd-b73a-4e4f-8845-4626d1078d90",
                "BPFActiveStageId": "fda8c64a-9269-446c-9988-eff5a6544839",
                "BPFActiveStageEntityName": "processstage",
                "BPFActiveStageLocalizedName": "Apply"
            }
        }
    }

While this may bog you down, you need “BPFFlowStageEntityRecordId” which is basically the GUID of the entity record. And how to get it? Quite easy. I use the “Get a record” action of Common data service (current environment) connector. In the Item ID, I use the below expression.

triggerBody()['entity']['entity']['BPFFlowStageEntityRecordId']
Business process flow - Flow step.

We have done the hard part already. We now have the Leave Request record information and the Business process flow instance record information. To complete the whole process, I send an email to the approver. Below is the screenshot of the whole flow in action.

 

Business process flow - Flow step.

The final piece in the puzzle is to use the flow we just created in the Business process flow step.

Business process flow - Flow step.

Once you update your business process flow and the requestor clicks on the Run flow button, our beloved flow fires.

Run flow from Business process flow.

And you should see our flow running successfully.

Run flow from Business process flow.

Hope this helps!

You may also like the below posts

Debajit Dutta
(Business Solutions MVP)