Sample: Work with business process flows
Microsoft Flow is now Power Automate. For more information, see this blog.
This content will be updated to reflect the branding change in the coming days.
This sample demonstrates how to programmatically work with business process flows such as retrieving the business process flow instances for an entity record, retrieving active path for a business process flow instance and its process stages, and changing the active stage. For information about these concepts, see Work with business process flows using code
This sample is available to download from Sample: Work with business process flows.
Before you can run the sample:
Have access to a Common Data Service environment.
Have appropriate privileges on the Lead, Opportunity, and Workflow entities and business process flow definition entity records used in this sample.
Have Visual Studio 2015 or later to run the sample.
Have Internet connection to download the sample project and to restore the NuGet packages used in the sample project.
What this sample does
Creates a sample Lead record. This automatically creates an instance of the "Lead To Opportunity Sales Process" business process flow for the Lead record.
Converts the Lead record to an Opportunity record.
Retrieves the business process flow instances associated with the "Opportunity" record using the
RetrieveProcessInstancesmessage. The first record in the returned collection is the active business process flow instance for the opportunity record, which is "Lead To Opportunity Sales Process" in this case.
Retrieves the active path and the process stages for the "Lead To Opportunity Sales Process" instance using the
Retrieves the currently active stage for the "Lead To Opportunity Sales Process" instance, and prompts the user whether to move to the next stage. On confirmation to move, sets the next stage in the active path as the active stage for the "Lead To Opportunity Sales Process" instance.
Finally, prompts the user whether to delete the records created during the sample run.
Here is the output of the sample:
Run the sample
Download the WorkWithBPF Visual Studio sample project, and extract it to a folder on your computer.
WorkWithBPF.slnfile in your extracted folder, and open it in Visual Studio.
The sample project uses NuGet packages that must be restored before running the sample. Ensure that automatic restore of NuGet packages is enabled in Visual Studio. More information: Enabling and disabling NuGet package restore
Alternatively, select Project > Manage NuGet Packages, and select Restore to manually restore the packages used in the sample.
Press F5 or select Debug > Start Debugging.
If you have not previously run one of the samples before, you’ll need to enter information to run the code, otherwise enter the number for one of the instances you have previously set up.
Prompt Description Enter a Dynamics 365 server name and port [crm.dynamics.com] Type the name of your Dynamics 365 server. The default is Dynamics 365 (online) (crm.dynamics.com) in North America.
Is this organization provisioned in Microsoft online services (y/n) [n] Type y if this is a Microsoft online services provisioned organization. Otherwise, type n. Enter domain\username Type your Microsoft account. Enter password Type your password. The characters will show as “*” in the window. Your password is securely saved in the Microsoft Credential Manager for later reuse. Specify an organization number (1-n)  From the list of organizations shown that you belong to, type the corresponding number. The default is 1, indicating the first organization in the list.
The sample will perform the operations described in What this sample does and may prompt you with additional options.
When the sample is complete, press ENTER to close the console window.