You might come across a requirement where you wan to open Close as Won or Close as Lost dialogue when user gets to last stage of business process flow on Opportunity form.
Out of the box button (‘Close as Won’) calls a JavaScript command to load this dialogue and we can call same command to load Close as Won dialouge
Following command is to trigger a JavaScript function on stage change of business process flow
Xrm.Page.data.process.addOnProcessStatusChange(statusOnChange);
You can check stage by calling following code
status = Xrm.Page.data.process.getStatus();
if (status == "finished")
DoSomeThing
To load a Close as Won dialogue you can use following code
Mscrm.OpportunityCommandActions.opportunityClose(1);
To load Close as Lost you can use following code
Mscrm.OpportunityCommandActions.opportunityClose(0);
I have used this in D365 and it works perfectly.
You might not need to loas this on finish stage of business process flow but you can call above code anywhere you need.
Below is the full code
function OnLoad() {
Xrm.Page.data.process.addOnProcessStatusChange(statusOnChange);
}
function statusOnChange() {
status = Xrm.Page.data.process.getStatus();
if (status == “finished”) {
Mscrm.OpportunityCommandActions.opportunityClose(1);
}
}
Hope this helps.