表格事件Onsave不执行承诺



我在Dynamics CRM中有一个Web资源,我正在尝试添加逻辑以执行保存。我正在使用addOnSave()方法将我的逻辑附加到保存。当我在保存逻辑中使用诺言时,保存&在保存完成之前,关闭页面退出页面。在关闭Web资源之前,我该如何使我的保存逻辑完全执行?

伪代码

Xrm.Event.addOnSave(function () {
  // Code makes it here
  Promise.all([promises]).then(function(){
    // Code never makes it here
    secondPromise.then(function(){
      showAlert();
      setTimeout(function(){
        closeAlert();
      }, 5000);
    });
  });
});

您想取消保存然后重新发行它,如下:

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);
    });
  });
});

响应您对预防违规的错误的评论,无法正确停止保存和关闭事件:使用xrmtoolbox的功能区工作台覆盖保存和关闭按钮,以指向一个可能看起来像这样的自定义功能:

function customSaveAndClose() {
  if (customSaveIsNeeded) {
    // execute your custom code
  } else {
    Xrm.Page.data.entity.save('saveandclose');
  }
}

您可以肯定地在应用功能区级别上覆盖S& c按钮,该级别将覆盖所有实体,但我相信您也可以一次仅对一个实体覆盖它。

如果您不想编辑色带(如果您以前从未做过它有点令人生畏),并且如果您对不支持的自定义没有严格的要求,也可以采取更轻松的途径只需覆盖mscrm.ribbonactions.saveandcloseform函数,这就是本机s& c按钮所调用的内容。看起来像这样:

// defined in /_static/_common/scripts/RibbonActions.js
Mscrm.RibbonActions.saveAndCloseForm = function() {
   // your code here
}

关于这种方法要注意的一些事情:

  • 它不受支持,可以破坏任何更新
  • CRM表单由多个帧组成,因此,如果您在自定义脚本中定义了该功能,并且不会执行该功能,请将您的定义更改为top.Mscrm而不是Mscrm
  • 如果您必须支持移动客户端,则应该避免使用此方法并覆盖功能区按钮。

最新更新