Here is the XRM code to move to next stage in Business Process Flow on desired condition. Add this code on ribbon button. If you want to add this code on Form save, then implement your logic of 'OnSave' accordingly.
Below code is using Opportunity entity. Other desired entities can be passed in successCallback function.
function MoveNextStage(stageToMove) {
var processId = Xrm.Page.getAttribute("processid").getValue();
// var currentStage = Xrm.Page.getAttribute("salesstage").getText();
// alert(currentStage);
var oDataURI = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/ProcessStageSet?$select=ProcessStageId,StageName&$filter=ProcessId/Id eq (guid'" + processId + "')";
var req = new XMLHttpRequest();
req.open("GET", encodeURI(oDataURI), true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
if (this.readyState == 4 /* complete */) {
req.onreadystatechange = null; //avoids memory leaks
if (this.status == 200) {
var stages = JSON.parse(this.responseText);
var counter = 0;
var findIndex = -1;
var findStage = stageToMove;
//if (currentStage == "Develop")
// findStage = "propose";
//else if (currentStage == "Qualify")
// findStage = "develop";
//alert(stages.d.results.length);
for (var i in stages.d.results) {
// alert("INFO " + findIndex + " findStage " + findStage + " ArrayStage " + stages.d.results[counter].StageName);
if (stages.d.results[counter].StageName == findStage) {
findIndex = counter;
alert("Matched " + findIndex + " findStage " + findStage + " ArrayStage " + stages.d.results[counter].StageName);
break;
}
counter++;
//alert(stages.d.results[counter].StageName);
}
var nextStageId = stages.d.results[findIndex].ProcessStageId;
Xrm.Page.getAttribute("stageid").setValue(nextStageId);
Xrm.Page.getAttribute("stageid").setSubmitMode('always');
//Xrm.Page.data.entity.save();
//window.location.reload(true);
} else {
//errorCallback();
}
}
};
req.send();
}
function InvokeSaveThen() {
Xrm.Page.data.save().then(successCallback, errorCallback);
Xrm.Page.data.entity.save();
}
function successCallback() {
//Needed to set form dirty to false explicitly as it is not done by platform
Xrm.Page.data.setFormDirty(false);
var Id = Xrm.Page.data.entity.getId();
Xrm.Utility.openEntityForm("opportunity", Id);
}
function errorCallback(attr1, attr2) {
}
#AW
Customization Consultant
Maison Consulting & Solutions