我正在为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);
}
}
跟踪服务打印出我所期望的一切。我是否还需要附加系统用户并以某种方式将实体引用附加到上下文中?
任何帮助表示赞赏。
看起来不错。我会放弃它。出于兴趣,为什么分配业主的过程不同?这不仅仅是像其他任何连接一样的数据库关系吗?在预先事件中调整儿童实体也是正确的,这是一种可接受的做法吗? – fluent 2012-07-16 08:25:09
不幸的是,这并没有什么区别。我相信我的问题在于尝试更改子实体的所有者,直接设置所有者,或者使用AssignRequest工作正常地在已注册为“主”实体的“帐户”上工作,但是当我尝试更改所有者孩子似乎被忽略了。 – fluent 2012-07-16 11:14:35