I did not get any error relevant to this issue. In the network, it looks like it is running first time and set the process flow and second time run , it gives script error.
Below is the code I'm using currently
Now , it runs and gives error on getid. "Cannot read property 'getId' of null"
function onload(executionContext) {
var formName = Xrm.Page.ui.formSelector.getCurrentItem().getLabel();
var formContext = executionContext.getFormContext();
var activeProcessID = formContext.data.process.getActiveProcess().getId();
alert(activeProcessID );
var flow1= "guid1";
var flow2= "guid2";
if (formName == "form1" && activeProcessID.toUpperCase() !=flow1) {
alert('change process flow');
formContext.data.process.setActiveProcess(flow1, callBackFunction);
formContext.data.save();
}
else if(formName == "form2" && activeProcessID.toUpperCase() !=flow2) {
alert('change process flow');
formContext.data.process.setActiveProcess(flow2, callBackFunction);
formContext.data.save();
}
};
function callBackFunction(result) {
if (result == "success") { } else { }
}