Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

PowerObjects Blog 

for Microsoft Business Applications

|

Business Process Flows: Programmatically Switch BPF in Dynamics 365 using JavaScript

Post Author: Joe D365 |

The ability to branch Business Process Flows (BPFs) is a very useful component of Dynamics 365. However, there may come a time when you need to change the BPF dynamically based on a specific field on the form. BPFs currently have a limitation on the number of stages (30 max) and the number of conditional levels (5 deep). If you hit one of these limitations, it could be useful to create multiple BPFs and use JavaScript to switch between processes depending on specific field values. In today’s blog, we will cover how to change the BPF using JavaScript in the unified interface when the form loads based on a custom field Type on the Opportunity form. Let’s take a closer look!

1. Create a new attribute on the form for the field that will change the BPF. In the example below, we have a new option set field called Type with values of New and Used. On create of the Opportunity, the Type field is locked and cannot be changed after the record has been created. When the Opportunity Type has a value of Used, update the business process flow to the Opportunity Used Type BPF.

BPF

2. Create two separate BPFs for the required entity. In this case, we created two separate BPFs for the Opportunity entity.

3. From each BPF, grab the record GUID from the URL. Write the values down as they will be referenced later in the JavaScript.

Opportunity Sales Process BPF:

BPF

Opportunity Used Type BPF (used only if the Opportunity Type attribute is set to Used):

BPF

4. Create a new Script Web Resource.

BPF

5. Add the following JavaScript to the Web Resource (be sure to update the attributes and GUIDs to the values that match your requirement):

function onLoadChangeBPFByType(executionContext) {
var formContext = executionContext.getFormContext();
var oppTopic = formContext.getAttribute(“name”).getValue();
var oppType = formContext.getAttribute(“jb_type”).getText();
var activeProcessID = formContext.data.process.getActiveProcess().getId();
if (oppTopic != null) {
if (oppType == “Used” && activeProcessID.toUpperCase() != “CBB4EDDC - B4AF - 4429 - AAE0 - 8 B404E00344F”) formContext.data.process.setActiveProcess(“CBB4EDDC - B4AF - 4429 - AAE0 - 8 B404E00344F”, callBackFunction);
}
};

function callBackFunction(result) {
if (result == “success”) {} else {}
}

6. Open the Opportunity Form editor and click Form properties.

7. Add the newly created JavaScript web resource to the form libraries and Event Handlers.

BPF

8. Click Save and Publish Customizations.

9. Create a new Opportunity with a Type of New and the BPF will remain set to Opportunity Sales Process.

BPF

10. Create a new Opportunity with a Type of Used and the BPF will automatically update to Opportunity Used Type once the Opportunity has been created.

BPF

11. The newly-created BPF Opportunity Used Type must be added to the unified interface apps that are to be used in your environment. In this example, we will just walk through adding the BPF to the Sales Hub.

12. In Dynamics 365, go to Settings > My Apps.

13. Click on the ellipsis next to Sales Hub, as shown below.

14. Click OPEN IN APP DESIGNER.

15. Click Components > ARTIFACTS > Business Process Flow.

16. Select the BPF you want to use in the Sales Hub app. In this example, Opportunity Used Type must be selected, as this is the newly-created BPF to be called by our JavaScript on the Opportunity form.

BPF
BPF

17. Click Save, Validate, and Publish.

BPF Branching adds a lot of flexibility, however if you run into a situation where you hit the 5 levels deep limitation or you need to create more than 30 stages, you can use the process described above to switch the process dynamically to the correct BPF.

Be sure to subscribe to our blog for more tips and tricks.

Happy Dynamics 365’ing!

Joe CRM
By Joe D365
Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.

PowerObjects Recommends