2012-07-13 85 views
1

我正在爲CRM 2011創建一個事件前插件,用於設置帳戶所有者並使用同一所有者更新所有子聯繫人。該插件已正確安裝並正確更新主要帳戶記錄,但子聯繫人所有者不會更改。我已將所有者名稱推入聯繫人的其他字段,以檢查我是否擁有正確的詳細信息,並且該字段正在更新。更新子實體/將實體連接到上下文

我敢肯定,它將兒童聯繫人附加到正確的上下文,但到目前爲止,我畫了一個空白。

//Set new account owner - Works fine 
account.OwnerId = new EntityReference(SystemUser.EntityLogicalName, ownerId); 

//Pass the same owner into the contacts - Does not get updated 
UpdateContacts(account.Id, ownerId, service, tracingService); 

系統正在成功更新帳戶所有者和子記錄的描述標籤。

public static void UpdateContacts(Guid parentCustomerId, Guid ownerId, IOrganizationService service, ITracingService tracingService) 
    { 
     // Create the FilterExpression. 
     FilterExpression filter = new FilterExpression(); 

     // Set the properties of the filter. 
     filter.FilterOperator = LogicalOperator.And; 
     filter.AddCondition(new ConditionExpression("parentcustomerid", ConditionOperator.Equal, parentCustomerId)); 

     // Create the QueryExpression object. 
     QueryExpression query = new QueryExpression(); 

     // Set the properties of the QueryExpression object. 
     query.EntityName = Contact.EntityLogicalName; 
     query.ColumnSet = new ColumnSet(true); 
     query.Criteria = filter; 

     // Retrieve the contacts. 
     EntityCollection results = service.RetrieveMultiple(query); 
     tracingService.Trace("Results : " + results.Entities.Count); 

     SystemUser systemUser = (SystemUser)service.Retrieve(SystemUser.EntityLogicalName, ownerId, new ColumnSet(true)); 
     tracingService.Trace("System User : " + systemUser.FullName); 

     XrmServiceContext xrmServiceContext = new XrmServiceContext(service); 

     for (int i = 0; i < results.Entities.Count; i++) 
     {     
      Contact contact = (Contact)results.Entities[i]; 
      contact.OwnerId = new EntityReference(SystemUser.EntityLogicalName, systemUser.Id); 
      contact.Description = systemUser.FullName; 

      xrmServiceContext.Attach(contact); 
      xrmServiceContext.UpdateObject(contact); 
      xrmServiceContext.SaveChanges(); 

      tracingService.Trace("Updating : " + contact.FullName); 
     } 
    } 

跟蹤服務打印出我所期望的一切。我是否還需要附加系統用戶並以某種方式將實體引用附加到上下文中?

任何幫助表示讚賞。

回答

4

您必須使用AssignRequest進行單獨的Web服務調用來更改記錄的所有權。不幸的是,你不能只改變Owner屬性。

+0

看起來不錯。我會放棄它。出於興趣,爲什麼分配業主的過程不同?這不僅僅是像其他任何連接一樣的數據庫關係嗎?在預先事件中調整兒童實體也是正確的,這是一種可接受的做法嗎? – fluent 2012-07-16 08:25:09

+0

不幸的是,這並沒有什麼區別。我相信我的問題在於嘗試更改子實體的所有者,直接設置所有者,或者使用AssignRequest工作正常地在已註冊爲「主」實體的「帳戶」上工作,但是當我嘗試更改所有者孩子似乎被忽略了。 – fluent 2012-07-16 11:14:35

0

我想我已經陷入了這種插件的各種混亂,因爲默認情況下更改帳戶所有者會自動更改關聯的聯繫人所有者。因此,我試圖覆蓋它已經在做的事情。

通過使用AssignRequest來設置帳戶所有者而不是子記錄它工作正常。克里斯給了我們信貸,他指出我正確的方向。

所需的只是將我的代碼的第一行更改爲使用AssignRequest,並且整個UpdateContacts方法變得比較流行。