Recently in my current project I have 2 Business Process Flow for the same entity and I have to activate or change the Business Process Flow on change of option set value. So for this I thought of writing this script.
function onload() {
if (Xrm.Page.ui.getFormType() != 1) {
// Read Origin Option text
var origin = Xrm.Page.data.entity.attributes.get("FieldName");
if (origin && origin.getText()) {
var caseOrigin = origin.getText();
// Get Business process flow id
var processFlow1Id = "xxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx";
var processFlow2Id = "xxxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxxxx";
var activeProcess = Xrm.Page.data.process.getActiveProcess();
var currProcessId = activeProcess.getId();
if (caseOrigin == "option set value or ID") {
if (currProcessId.toLowerCase() != processFlow1Id.toLowerCase()) {
// Switch to the “Business Process Flow 1”
Xrm.Page.data.process.setActiveProcess(processFlow1Id, myCallBack);
}
} else {
if (currProcessId.toLowerCase() != processFlow2Id.toLowerCase()) {
// Switch to the “Business Process Flow 2”
Xrm.Page.data.process.setActiveProcess(processFlow2Id.toLowerCase(), myCallBack);
}
}
}
}
}
// Call back function post Process flow switch
function myCallBack(response) {
if (response == "success") {
Xrm.Page.data.entity.save();
}
}
Create a new Web Resource and copy paste the above code into it and call the onload() function on load of page.