表单事件OnSave没有执行Promise [英] Form Event OnSave not executing Promise

查看:107
本文介绍了表单事件OnSave没有执行Promise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Dynamics CRM中有一个Web资源,我试图在其中添加要在保存时执行的逻辑.我正在使用 addOnSave()方法将逻辑附加到保存.当我在保存逻辑中使用Promise时,Save&保存完成之前,关闭"退出页面.在关闭网络资源之前,如何使我的保存逻辑完全执行?

I have a web resource in Dynamics CRM where I am trying to add logic to execute on save. I am using the addOnSave() method to attach my logic to the save. When I use a Promise in my save logic, Save & Close exits the page before my save completes. How can I get my save logic to fully execute before the web resource is closed?

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

推荐答案

您要取消保存,然后重新发出,就像这样:

You want to cancel the save and then reissue it, like this:

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

针对您对preventDefault无法正确停止Save and Close事件的错误的评论,请执行以下操作:使用XrmToolbox中的Ribbon Workbench覆盖Save and Close按钮,以指向自定义函数,该函数可能看起来像这样:

In response to your comment about the bug where preventDefault doesn't properly stop a Save and Close event: use the Ribbon Workbench from the XrmToolbox to override the Save and Close button to point to a custom function which might look something like this:

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

您可以肯定地在应用程序功能区"级别覆盖S& C按钮,它将对所有实体都覆盖它,但是我相信您也可以一次仅对一个实体覆盖它.

You can for sure override the S&C button at the Application Ribbon level which would override it for all entities, but I believe you can override it for just one entity at a time as well.

如果您不想弄乱功能区的编辑(如果您以前从未做过,那会有些吓人),并且如果您对不受支持的自定义项没有严格的要求,那么您也可以采用更简单的方法只需覆盖Mscrm.RibbonActions.saveAndCloseForm函数,该函数就是本机S& C按钮调用的函数.看起来像这样:

If you don't want to mess with editing the ribbon (it's a little intimidating if you've never done it before) and if you don't have strict requirements regarding unsupported customizations, you can also take the easier route of simply overriding the Mscrm.RibbonActions.saveAndCloseForm function which is what the native S&C buttons call. That would look something like this:

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

有关此方法的一些注意事项:

Some things to note about this approach:

  • 不支持,可能会随着任何更新而中断
  • CRM表单由多个框架组成,因此,如果您在自定义脚本中定义了该函数且该函数没有执行,请将您的定义更改为 top.Mscrm 而不是仅将 Mscrm .
  • 如果必须支持移动客户端,则应该避免这种方法,而应改用功能区按钮.

这篇关于表单事件OnSave没有执行Promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆