发生异常时撤消表单更改

时间:2016-06-19 21:37:06

标签: javascript dynamics-crm dynamics-crm-2013

在Dynamics CRM 2013中,是否可以在发生业务流程错误时还原表单上更改的字段?

For example:
1. User changes a text field on a form from 'abc' to 'xyz'
2. User clicks save
3. CRM pre-operation plugin validates field, 'xyz' not allowed, exception   thrown
4. CRM displays business process error to user, 'xyz' is not allowed
5. The value 'xyz' is still shown in the form

我们想要的行为是'xyz'在步骤5中恢复为'abc'。

2 个答案:

答案 0 :(得分:2)

您需要先缓存数据。你可以这样做OnLoad,例如通过记忆实体的属性值:

function GetInitialAttributeState() {
   var preImage = {};

   Xrm.Page.data.entity.attributes.forEach(function(field) {
      // TODO: for lookup attributes you need to do extra work in order to avoid making merely a copy of an object reference.
      preImage[field.getName()] = field.getValue();
   });

   return preImage;
}

window.preImage = GetInitialAttributeState();

然后,您需要通过Xrm.Page.data.save方法执行保存操作。传递回调函数处理错误并重置字段,例如

Xrm.Page.data.save().then(
   function() {
      /* Handle success here. */
      window.preImage = getInitialAttributeState();
   },
   function() {
      /* Handle errors here. */
      Xrm.Page.data.entity.attributes.forEach(function(field) {
         if (field.getIsDirty()) {
            field.setValue(preImage[field.getName()]);
         }
      });
   });

无法使用save事件以这种方式重置表单的字段,因为它在实际保存操作之前启动,而不是在它之后启动。

答案 1 :(得分:0)

为什么让用户保存记录?

您可以使用商务标尺来验证字段,并针对您不喜欢"喜欢"的值设置字段的错误条件。错误条件将持续存在并阻止它们保存记录,直到它们更改值。错误消息可以让他们解释为什么它们的值无效。

显然,您可以在业务规则中进行的验证是有限的,但您的示例并没有明确我们匹配的基础" xyz"作为一个"坏"值。