Control Next and Previous stage movement in Dynamics 365/ CDS Business Process flows using Client API / PowerApps
Honestly I was not aware of this function sometime back and this was purely an accidental discovery. And guess what when I queried around, I found that it actually missed the cognizance of so many consultants out there. And while this function have been introduced, we still keep telling the customer that it is kind of not possible to show something like a user confirmation before stage movement.
You may argue that “OnStageChange” event has been there from the beginning. However the issue with it is, it fires when the stage have already changed. Hence does not make any sense.
In the below example I am going to show you couple of scenarios. And trust me if you are not aware of this and seeing for the first time, it will be a huge sense of relief for you since this is a kind of requirement which pops up every now and then.
- Getting user confirmation before the stage movement happens
- Stop back stage movement.
Below is the sample code that I have set up for account entity form
var Acc = {};
Acc.formEvents = {
stageMovementConfirmed: false,
form_load: function (e) {
var fc = e.getFormContext();
fc.data.process.removeOnPreStageChange(Acc.formEvents.handlePreStage);
fc.data.process.addOnPreStageChange(Acc.formEvents.handlePreStage);
},
handlePreStage: function (e) {
debugger;
// get the event arguments
var bpfArgs = e.getEventArgs();
if (bpfArgs.getDirection() === "Previous") // back stage movement is not allowed;
{
bpfArgs.preventDefault();
var alertStrings = { confirmButtonLabel: "OK", text: "Back stage movement is not allowed", title: "Sample title" };
var alertOptions = { height: 120, width: 260 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
return;
}
// this will fire again when move next is called.
// so if stage movement has been allowed earlier, please proceed. else ask for user confirmation.
if (Acc.formEvents.stageMovementConfirmed === true) {
Acc.formEvents.stageMovementConfirmed = false;
return;
}
if (bpfArgs.getDirection() === "Next") { // only next stage movement is allowed.
// stop the stage movement
bpfArgs.preventDefault();
var confirmStrings = { text: "Are you sure you want to move to the next stage?", title: "Stage movement Confirmation!" };
var confirmOptions = { height: 150, width: 300 };
Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
function (success) {
if (success.confirmed) {
Acc.formEvents.stageMovementConfirmed = true;
e.getFormContext().data.process.moveNext();
}
});
}
// you can also play with the other properties of eventargs
// get the stage - bpfArgs.getStage();
// get the steps - bpfArgs.getStage().getSteps();
}
};
Well, it’s a very simple code. The form_load event is fired on account form load and registers the function handlePreStage on Business process “onPreStageChange” event.
The remaining part is self explanatory and I have basically put all the code with appropriate comments to highlight what all you can do with this function.
All set and done, below is the experience I get when I try this out.
posted on 2021-07-28 08:01 lingdanglfw 阅读(74) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2009-07-28 在QueryRange里面慎用strfmt拼出来的条件