Javascript 保存上的表单事件未执行承诺
我在Dynamics CRM中有一个web资源,我正在尝试添加保存时执行的逻辑。我正在使用Javascript 保存上的表单事件未执行承诺,javascript,dynamics-crm,microsoft-dynamics,webresource,Javascript,Dynamics Crm,Microsoft Dynamics,Webresource,我在Dynamics CRM中有一个web资源,我正在尝试添加保存时执行的逻辑。我正在使用addOnSave()方法将我的逻辑附加到save。当我在保存逻辑中使用承诺时,save&Close会在保存完成之前退出页面。如何在关闭web资源之前完全执行保存逻辑 伪码 您想取消保存,然后重新发布,如下所示: Xrm.Page.data.entity.addOnSave(function (context) { var eventArgs = context.getEventArgs(); ev
addOnSave()
方法将我的逻辑附加到save。当我在保存逻辑中使用承诺时,save&Close会在保存完成之前退出页面。如何在关闭web资源之前完全执行保存逻辑
伪码
您想取消保存,然后重新发布,如下所示:
Xrm.Page.data.entity.addOnSave(function (context) {
var eventArgs = context.getEventArgs();
eventArgs.preventDefault(); // cancels the save (but other save handlers will still run)
Promise.all([promises]).then(function(){
// Code never makes it here
secondPromise.then(function(){
showAlert();
setTimeout(function(){
closeAlert();
// reissue the save
Xrm.Page.data.entity.save('saveandclose');
}, 5000);
});
});
});
function customSaveAndClose() {
if (customSaveIsNeeded) {
// execute your custom code
} else {
Xrm.Page.data.entity.save('saveandclose');
}
}
// defined in /_static/_common/scripts/RibbonActions.js
Mscrm.RibbonActions.saveAndCloseForm = function() {
// your code here
}
针对您关于preventDefault无法正确停止保存和关闭事件的错误的评论:使用XrmToolbox中的Ribbon Workbench覆盖保存和关闭按钮,以指向一个自定义函数,该函数可能如下所示:
Xrm.Page.data.entity.addOnSave(function (context) {
var eventArgs = context.getEventArgs();
eventArgs.preventDefault(); // cancels the save (but other save handlers will still run)
Promise.all([promises]).then(function(){
// Code never makes it here
secondPromise.then(function(){
showAlert();
setTimeout(function(){
closeAlert();
// reissue the save
Xrm.Page.data.entity.save('saveandclose');
}, 5000);
});
});
});
function customSaveAndClose() {
if (customSaveIsNeeded) {
// execute your custom code
} else {
Xrm.Page.data.entity.save('saveandclose');
}
}
// defined in /_static/_common/scripts/RibbonActions.js
Mscrm.RibbonActions.saveAndCloseForm = function() {
// your code here
}
当然,您可以在应用程序功能区级别覆盖S&C按钮,这将覆盖所有实体的S&C按钮,但我相信您也可以一次覆盖一个实体的S&C按钮
如果您不想编辑功能区(如果您以前从未这样做过,这会有点吓人),如果您对不受支持的自定义项没有严格的要求,您也可以采取更简单的方法,简单地覆盖Mscrm.RibbonActions.saveAndCloseForm函数,这是本机s&C按钮所调用的函数。看起来是这样的:
Xrm.Page.data.entity.addOnSave(function (context) {
var eventArgs = context.getEventArgs();
eventArgs.preventDefault(); // cancels the save (but other save handlers will still run)
Promise.all([promises]).then(function(){
// Code never makes it here
secondPromise.then(function(){
showAlert();
setTimeout(function(){
closeAlert();
// reissue the save
Xrm.Page.data.entity.save('saveandclose');
}, 5000);
});
});
});
function customSaveAndClose() {
if (customSaveIsNeeded) {
// execute your custom code
} else {
Xrm.Page.data.entity.save('saveandclose');
}
}
// defined in /_static/_common/scripts/RibbonActions.js
Mscrm.RibbonActions.saveAndCloseForm = function() {
// your code here
}
此方法需要注意的一些事项:
- 它不受支持,可能会因任何更新而中断
- CRM表单由多个框架组成,因此,如果您在自定义脚本中定义了该函数,但未执行该函数,请将定义更改为
,而不仅仅是top.Mscrm
Mscrm
- 如果您必须支持移动客户端,您可能应该避免这种方法,而是覆盖功能区按钮
Xrm.Page.data.entity.addOnSave(function (context) {
var eventArgs = context.getEventArgs();
eventArgs.preventDefault(); // cancels the save (but other save handlers will still run)
Promise.all([promises]).then(function(){
// Code never makes it here
secondPromise.then(function(){
showAlert();
setTimeout(function(){
closeAlert();
// reissue the save
Xrm.Page.data.entity.save('saveandclose');
}, 5000);
});
});
});
function customSaveAndClose() {
if (customSaveIsNeeded) {
// execute your custom code
} else {
Xrm.Page.data.entity.save('saveandclose');
}
}
// defined in /_static/_common/scripts/RibbonActions.js
Mscrm.RibbonActions.saveAndCloseForm = function() {
// your code here
}
针对您关于preventDefault无法正确停止保存和关闭事件的错误的评论:使用XrmToolbox中的Ribbon Workbench覆盖保存和关闭按钮,以指向一个自定义函数,该函数可能如下所示:
Xrm.Page.data.entity.addOnSave(function (context) {
var eventArgs = context.getEventArgs();
eventArgs.preventDefault(); // cancels the save (but other save handlers will still run)
Promise.all([promises]).then(function(){
// Code never makes it here
secondPromise.then(function(){
showAlert();
setTimeout(function(){
closeAlert();
// reissue the save
Xrm.Page.data.entity.save('saveandclose');
}, 5000);
});
});
});
function customSaveAndClose() {
if (customSaveIsNeeded) {
// execute your custom code
} else {
Xrm.Page.data.entity.save('saveandclose');
}
}
// defined in /_static/_common/scripts/RibbonActions.js
Mscrm.RibbonActions.saveAndCloseForm = function() {
// your code here
}
当然,您可以在应用程序功能区级别覆盖S&C按钮,这将覆盖所有实体的S&C按钮,但我相信您也可以一次覆盖一个实体的S&C按钮
如果您不想编辑功能区(如果您以前从未这样做过,这会有点吓人),如果您对不受支持的自定义项没有严格的要求,您也可以采取更简单的方法,简单地覆盖Mscrm.RibbonActions.saveAndCloseForm函数,这是本机s&C按钮所调用的函数。看起来是这样的:
Xrm.Page.data.entity.addOnSave(function (context) {
var eventArgs = context.getEventArgs();
eventArgs.preventDefault(); // cancels the save (but other save handlers will still run)
Promise.all([promises]).then(function(){
// Code never makes it here
secondPromise.then(function(){
showAlert();
setTimeout(function(){
closeAlert();
// reissue the save
Xrm.Page.data.entity.save('saveandclose');
}, 5000);
});
});
});
function customSaveAndClose() {
if (customSaveIsNeeded) {
// execute your custom code
} else {
Xrm.Page.data.entity.save('saveandclose');
}
}
// defined in /_static/_common/scripts/RibbonActions.js
Mscrm.RibbonActions.saveAndCloseForm = function() {
// your code here
}
此方法需要注意的一些事项:
- 它不受支持,可能会因任何更新而中断
- CRM表单由多个框架组成,因此,如果您在自定义脚本中定义了该函数,但未执行该函数,请将定义更改为
,而不仅仅是top.Mscrm
Mscrm
- 如果您必须支持移动客户端,您可能应该避免这种方法,而是覆盖功能区按钮