Javascript 保存上的表单事件未执行承诺

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

我在Dynamics CRM中有一个web资源,我正在尝试添加保存时执行的逻辑。我正在使用
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
  • 如果您必须支持移动客户端,您可能应该避免这种方法,而是覆盖功能区按钮

这对我不起作用。原始保存和关闭从未被阻止。我测试了这一点,将阻止代码放入,然后再也没有重新发出保存,但屏幕仍然关闭并保存。更多的研究表明,这将阻止保存,但不会阻止关闭。在完成对web资源的自定义保存之前,我会遇到同样的问题,即表单将退出。目前,我们已删除“保存并关闭”按钮,以便用户在保存后必须离开页面。我们还添加了一个弹出框来训练用户在导航离开之前等待保存完成。在某个时候,我们可能会回来定制一个按钮来执行保存和关闭。这对我不起作用。原始保存和关闭从未被阻止。我测试了这一点,将阻止代码放入,然后再也没有重新发出保存,但屏幕仍然关闭并保存。更多的研究表明,这将阻止保存,但不会阻止关闭。在完成对web资源的自定义保存之前,我会遇到同样的问题,即表单将退出。目前,我们已删除“保存并关闭”按钮,以便用户在保存后必须离开页面。我们还添加了一个弹出框来训练用户在导航离开之前等待保存完成。在某个时候,我们可能会回来定制一个按钮来执行保存和关闭。