審批通過后的單據,一般要對其進行控制,不能修改,不能添加,刪除等,下面分別介紹下如何實現:
一、 禁止修改:
1. 主表控制,如果頁面上審批狀態為審批中或審批通過,就把整個頁面都disable掉
1: function controlReadonly() {
2: var status = Xrm.Page.data.entity.attributes.get("new_approval_status").getValue();3: if (status == 2 || status == 3)//審批中或者審批通過
4: {5: var controls = Xrm.Page.ui.controls.get();
6: for (var i in controls) {7: var control = controls[i];8: control.setDisabled(true);
9: }10: }11: }
2. 子表控制, 如果有子表,也要讓其不能修改
首先查找主表的approval status,如果是審批中或審批通過就disable整個子表
1: function disableSubForm() {
2: var marketingPlan = Xrm.Page.getAttribute("new_marketing_planid").getValue();
3: if (marketingPlan != null) {
4: var filter = "new_marketing_planSet?$select=new_approval_status&$filter=new_marketing_planId eq guid'" + marketingPlan[0].id + "'";5: var status = Query_ent(filter);6: if (status != null && status.new_approval_status != null) {
7: if (status.new_approval_status.Value == 3 || status.new_approval_status.Value == 2) {
8: controlReadOnly();9: }10: }11: }12: }
二、禁止添加及刪除子表記錄
通過上面的js控制,頁面上的所有字段確實被disable了,但子表的subgrid及子表頁面上的添加及刪除按鈕沒有被控制住。所以決定寫一個通用的插件來控制添加及刪除。1. 下圖顯示了如何在主表上注冊這個通用插件![]()
這里 message為Delete,是對delete的控制;如果是Create,就要設置成post-operation,才能對添加進行控制。
重點是右邊的Unsecure Configuration的配置:
- 傳入的條件
<filter type="or">
<condition attribute="new_approval_status" operator="eq" value="2" />
<condition attribute="new_approval_status" operator="eq" value="3" />
</filter>
- 如果滿足這個條件就報這個error
<check error="Cannot delete when approval status is approving or approved !">
2. 子表上注冊
子表上沒有approval status這個字段,這個字段只在主表上有,但又要依賴這個字段來控制子表,怎么辦?
這時就要指定進行如下的配置了:
1: <check error="Cannot delete sub-form when main form is approving or approved!">2: <link-entity name="new_marketing_plan" to="new_marketing_planid">3: <filter type="or">
4: <condition attribute="new_approval_status" operator="eq" value="2" />5: <condition attribute="new_approval_status" operator="eq" value="3" />6: </filter>7: </link-entity>8: </check>
簡單介紹下這里的配置,跟主表的配置相比,只是多了個link-entity:<link-entity name="new_marketing_plan" to="new_marketing_planid">name指的是主表的實體, to指的是子表中link到主表實體的字段
三、 技術實現
1. 通過一個參數的構造函數來讀取unsecure configuration里的配置
1: public EntityCheck(string unsecure)2: {3: m_Config = unsecure;4: }
Microsoft Dynamics CRM 平台支持可選的插件構造函數,該函數接受一個或兩個字符串參數。如果編寫此類構造函數,則可以在運行時向插件傳遞任何信息字符串。以下示例顯示構造函數的格式。在此示例中,插件類名為 SamplePlugin。public SamplePlugin() public SamplePlugin(string unsecure) public SamplePlugin(string unsecure, string secure)構造函數的第一個字符串參數包含公共(不安全)信息。第二個字符串參數包含非公共(安全)信息。在這里,安全是指加密的值,而不安全是指未加密的值。如果使用帶脫機訪問功能的 Microsoft Dynamics CRM for Microsoft Office Outlook,則 CRM for Outlook 脫機時安全字符串不會傳遞到執行的插件。
在這些字符串中傳遞到插件構造函數的信息是在 Microsoft Dynamics CRM 中注冊插件時指定的。使用插件注冊工具注冊插件時,可以在“注冊新步驟”窗體提供的“安全配置”和“不安全配置”字段中輸入安全信息和不安全信息。使用 Microsoft Dynamics CRM SDK 以編程方式注冊插件時,SdkMessageProcessingStep.Configuration 包含不安全的值,SdkMessageProcessingStep.SecureConfigId 引用包含安全值的 SdkMessageProcessingStepSecureConfig 記錄。
2. 讀取link-entity1: XmlNodeList linkEntityNodeList = linkEntityNode.SelectNodes("link-entity");
2: if (linkEntityNodeList != null)
3: {4: foreach (XmlNode subLinkEntityNode in linkEntityNodeList)
5: {6: LinkEntity subLe = GetLinkEntity(subLinkEntityNode);7: le.LinkEntities.Add(subLe);8: }9: }
3. 從config里讀取filter1: XmlNodeList filterNodeList = linkEntityNode.SelectNodes("filter");
2: if (filterNodeList != null)
3: {4: foreach (XmlNode filterNode in filterNodeList)
5: {6: FilterExpression filter = this.GetFilter(filterNode);7:8: le.LinkCriteria.AddFilter(filter);9: }10: }
4. 從config里讀取condition1: XmlNodeList conditionNodeList = linkEntityNode.SelectNodes("condition");
2: if (conditionNodeList != null)
3: {4: foreach (XmlNode conditionNode in conditionNodeList)
5: {6: ConditionExpression condition = this.GetCondition(conditionNode);7:8: le.LinkCriteria.AddCondition(condition);9: }10: }
5. 最后合成一個QueryExpression對象,傳入到RetrieveMultiple方法,進行查詢,如果有記錄存在,就拋出error:1: EntityCollection ec = AdminService.RetrieveMultiple(config.Query);2: if (ec != null && ec.Entities.Count > 0)
3: {4: throw new InvalidPluginExecutionException(config.Error);5: }