2016-06-19 29 views
3

在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」恢復到「ABC」步驟5

+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事件這樣重置表單的領域,因爲它實際的保存操作前踢,後從來沒有它。

0

爲什麼讓用戶保存記錄呢?

你可以使用一個業務魯埃爾驗證字段,並設置了一個錯誤條件對字段值你不「喜歡」。錯誤情況將持續存在並阻止他們保存記錄,直到他們更改值。錯誤消息可以給他們一些解釋,爲什麼他們的價值是無效的。

很明顯,你可以在業務規則做驗證是有限的,但你的例子不說清楚在什麼基礎上我們匹配「XYZ」是「壞」的價值。

+0

該示例是對問題的簡化。這就是說,亨克的回答正是我所需要的。 – user329847