简体   繁体   English

如何防止表单在 Dynamics 365 中保存

[英]How can I prevent a form from saving in Dynamics 365

I want to prevent a form from saving if the entered "dni" value is already registered by another "person", I tried to keep the form from saving in its onSave event, and then if my condition matched, I'd make the form save then.如果输入的“dni”值已被另一个“人”注册,我想阻止表单保存,我试图阻止表单保存在其 onSave 事件中,然后如果我的条件匹配,我会制作表单然后保存。 So I made the following code:所以我做了以下代码:

function saveDni(executionContext) {
  try {
    const formContext = executionContext.getFormContext();
    const currentDni = formContext.getAttribute("cr6ff_dni").getValue();
    
    executionContext.getEventArgs().preventDefault();

    var fetchXml = [
      "<fetch top='1'>",
      "  <entity name= 'cr6ff_person'>",
      "    <filter>",
      "      <condition attribute='cr6ff_dni' operator='eq' value='", currentDni, "'/>",
      "    </filter>",
      "  </entity>",
      "</fetch>",
    ].join("");

    fetchXml = "?fetchXml=" + encodeURIComponent(fetchXml);

    Xrm.WebApi.retrieveMultipleRecords("cr6ff_person", fetchXml).then(
      (result) => {
        if (result.entities.length == 0) {
          formContext.data.entity.save();
        }
     }
   ) 
}
catch(error) {
  console.log("An error has occurred while validating the user's data.");
}

The thing is, when the save() method triggers, it also triggers this function back again, so I'm stuck in a loop.问题是,当 save() 方法触发时,它也会再次触发这个 function,所以我陷入了一个循环。 Any ideas as to how I could work this out?关于我如何解决这个问题的任何想法?

Let me recommend you the best practices.让我向您推荐最佳实践。

  1. Avoid Asynchronous calls like Xrm.WebApi.retrieveMultipleRecords in form save for better User experience.避免像Xrm.WebApi.retrieveMultipleRecords这样的异步调用形式保存以获得更好的用户体验。 Read more 阅读更多
  2. formContext.data.entity.save() is deprecated, use formContext.data.save() instead. formContext.data.entity.save()已弃用,请改用formContext.data.save() Reference 参考
  3. Better to move the business logic validation to plugin or flow最好将业务逻辑验证移至插件或流

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想在Dynamics 365 crm中从一种形式重定向到另一种形式 - I want to redirect from one form to another in dynamics 365 crm 如何在 MS Dynamics 365 中自动填写表单? - How do I automate form filling in MS Dynamics 365? 如何使用 Dynamics 365 的保存和路由按钮? - How can I work with save and route button Dynamics 365? 将 email 上的“至”字段设置为动态 365 - Set "to" field on email form i dynamics 365 Dynamics 365版本9中的表单上下文 - Form Context in Dynamics 365 version 9 如何在 Dynamics 365 on-premises 的日期时间字段中限制最小和最大小时数? - How can I restrict minimum and maximum hours in a datetime field in Dynamics 365 on-premise? 如何在Microsoft dynamics crm 365中通过javascript获取查找字段的值 - How can I get the value of a lookup field by javascript in Microsoft dynamics crm 365 如何将 javascript 添加到 Microsoft Dynamics 365 中的 web 表单 - how to add javascript to a web form in Microsoft Dynamics 365 如何从Dynamics 365 CRM在线调用第三方REST服务? - How do I call 3rd party REST service from Dynamics 365 CRM online? 如何防止没有管理员访问权限的用户保存状态为一定的表单 - How do I prevent users without admin access from saving a form with a certain status in faces
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM