Home » Dynamically Updating a Business Process Flow in Dynamics 365

Dynamically Updating a Business Process Flow in Dynamics 365

by Laura Brewer
5 minutes read

There may be situations where you want to have more control over the functionality behind a business process flow in Dynamics 365. Business process flows (BPF) make it possible to control the system behind the evolution of a record. A few examples of business flows include those found on a Lead, Opportunity, or Quote within Dynamics 365.

The out-of-box BPF in Dynamics 365 usually serve as a great starting point or framework for driving progress. Often your business will need to develop a different format for each process flow. In the event you have multiple BPF’s for an entity in your system, you may want to dynamically change the BPF based on certain parameters or trigger points.

This blog from the developer team at Dynamic Consultants Group explains how to set the BPF on an entity (opportunity) in CRM based on a trigger field, using a plugin developed in C#.

Setting the Business Process Flow Through Logic

This particular C# class is where the actual plugin logic lies (this is where you want your magic to happen)!

First, don’t forget to set up your organizational service connections in your code, as well as your execution logic. In this example, we have an option that the user selects on the creation of the opportunity, which is then supposed to set the correct BPF for the record. Your situation may be different, but the concept should be similar.

Business Process Flow

//start Plugin Logic Code//

try

{

//INITIALIZE VARIABLE TO STORE THE VALUE OF THE BUSINESS PROCESS OPTION SET VALUE SELECTED ON THE OPPORTUNITY

var businessProcessOptionSetValue = ((OptionSetValue)PrimaryEntity[“entityname”]).Value;

// throw new Exception(“ the value of the option set is: “ + businessProcessOptionSetValue.ToString());

//BUSINESS PROCESS 1 SELECTED

if (businessProcessOptionSetValue == 1000001) {

SetProcessRequest request = new SetProcessRequest();

request.Target = new EntityReference(“opportunity”, PrimaryEntity.Id);

// THE BELOW POINTS TO THE CORRECT GUID OF THE BUSINESS PROCESS FLOW

request.NewProcess = new EntityReference(“workflow”, new Guid(“business processflowid”));

ElevatedOrganizationService.Execute(request);

}

//BUSINESS PROCESS 2 SELECTED

else if (businessProcessOptionSetValue == 1000002)

{

SetProcessRequest request = new SetProcessRequest();

request.Target = new EntityReference(“opportunity”, PrimaryEntity.Id);

// THE BELOW POINTS TO THE CORRECT GUID OF THE BUSINESS PROCESS FLOW

request.NewProcess = new EntityReference(“workflow”, new Guid(“business processflowid”));

ElevatedOrganizationService.Execute(request);

}

//BUSINESS PROCESS 3 SELECTED

else if (businessProcessOptionSetValue == 1000003)

{

SetProcessRequest request = new SetProcessRequest();

request.Target = new EntityReference(“opportunity”, PrimaryEntity.Id);

// THE BELOW POINTS TO THE CORRECT GUID OF THE BUSINESS PROCESS FLOW

request.NewProcess = new EntityReference(“workflow”, new Guid(“business processflowid”));

ElevatedOrganizationService.Execute(request);

}

//BUSINESS PROCESS 4 SELECTED

else if (businessProcessOptionSetValue == 1000004)

{

SetProcessRequest request = new SetProcessRequest();

request.Target = new EntityReference(“opportunity”, PrimaryEntity.Id);

// THE BELOW POINTS TO THE CORRECT GUID OF THE BUSINESS PROCESS FLOW

request.NewProcess = new EntityReference(“workflow”, new Guid(“business processflowid”));

ElevatedOrganizationService.Execute(request);

}

//BUSINESS PROCESS 5 SELECTED

else if (businessProcessOptionSetValue == 1000005)

{

SetProcessRequest request = new SetProcessRequest();

request.Target = new EntityReference(“opportunity”, PrimaryEntity.Id);

// THE BELOW POINTS TO THE CORRECT GUID OF THE BUSINESS PROCESS FLOW

request.NewProcess = new EntityReference(“workflow”, new Guid(“business processflowid”));

ElevatedOrganizationService.Execute(request);

}

//BUSINESS PROCESS 6 SELECTED

else if (businessProcessOptionSetValue == 1000006)

{

SetProcessRequest request = new SetProcessRequest();

request.Target = new EntityReference(“opportunity”, PrimaryEntity.Id);

// THE BELOW POINTS TO THE CORRECT GUID OF THE BUSINESS PROCESS FLOW

request.NewProcess = new EntityReference(“workflow”, new Guid(“business processflowid”));

ElevatedOrganizationService.Execute(request);

}

}

catch (Exception)

{

throw;

}

}

}

}

//end Plugin Logic Code

Conclusion

This C# class is an example of flexible development of plugins for Dynamics 365. In this example we showed how to set the business process flow (BPF) on the Opportunity entity based on an option set value selected on the Opportunity record. We would love to hear that this helped you as it has helped us! Let us know what you think and happy coding!

You are on the blog right now. If you are interested in our consulting services, visit our website to learn more!