Dynamics crm 2011 更新子实体/将实体附加到上下文

Dynamics crm 2011 更新子实体/将实体附加到上下文,dynamics-crm-2011,Dynamics Crm 2011,我正在为CRM 2011创建一个事件前插件,用于设置帐户所有者并更新同一所有者的所有子联系人。插件已正确安装并正确更新主帐户记录,但子联系人所有者未更改。我已将所有者姓名推入联系人的另一个字段,以检查我是否有正确的详细信息,并且该字段正在更新 我确信这与将子联系人附加到正确的上下文有关,但到目前为止,我已经画了一个空白 //Set new account owner - Works fine account.OwnerId = new EntityReference(SystemUser.Ent

我正在为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);
        }
    }
public static void UpdateContacts(Guid parentCustomerId、Guid ownerId、IOOrganizationService服务、ITracingService tracingService)
{
//创建FilterExpression。
FilterExpression filter=新的FilterExpression();
//设置过滤器的属性。
filter.FilterOperator=逻辑运算符和;
AddCondition(新的ConditionExpression(“parentcustomerid”,ConditionOperator.Equal,parentcustomerid));
//创建QueryExpression对象。
QueryExpression query=新建QueryExpression();
//设置QueryExpression对象的属性。
query.EntityName=Contact.EntityLogicalName;
query.ColumnSet=新列集(true);
query.Criteria=filter;
//检索联系人。
EntityCollection结果=service.RetrieveMultiple(查询);
tracingService.Trace(“结果:+Results.Entities.Count”);
SystemUser SystemUser=(SystemUser)service.Retrieve(SystemUser.EntityLogicalName,ownerId,new ColumnSet(true));
tracingService.Trace(“系统用户:“+systemUser.FullName”);
XrmServiceContext XrmServiceContext=新的XrmServiceContext(服务);
对于(int i=0;i
跟踪服务打印出我所期望的一切。我是否还需要附加系统用户并以某种方式将实体引用附加到上下文


感谢您的帮助。

您必须使用单独的web服务调用来更改记录的所有权。不幸的是,您不能仅仅更改Owner属性。

您必须使用单独的web服务调用来更改记录的所有权。不幸的是,你不能只更改所有者属性。

我想我已经把这个插件弄得一团糟了,因为默认情况下更改帐户所有者会自动更改相关联系人的所有者。因此,我试图覆盖它已经在做的事情

通过使用AssignRequest设置帐户所有者而不是子记录,它工作得很好。克里斯给我指出了正确的方向


所需要做的只是将我的代码的第一行更改为使用AssignRequest,整个UpdateContacts方法就被禁用了。

我想我正在使用这个插件,因为默认情况下更改帐户所有者会自动更改相关联系人的所有者。因此,我试图覆盖它已经在做的事情

通过使用AssignRequest设置帐户所有者而不是子记录,它工作得很好。克里斯给我指出了正确的方向


所需要的只是将我的代码的第一行更改为使用AssignRequest,整个UpdateContacts方法就变成了obselete。

看起来不错。我要试一试。只是出于兴趣,为什么分配所有者的过程不同?它不只是一个数据库关系吗?此外,在事前调整子实体是否正确,这是一种可接受的做法吗?不幸的是,这没有任何区别。我认为我的问题在于试图更改子实体的所有者,直接设置所有者或对注册为“主要”实体的“帐户”使用AssignRequest work fine,但当我尝试更改子实体的所有者时,它似乎被忽略。看起来不错。我要试一试。只是出于兴趣,为什么分配所有者的过程不同?它不只是一个数据库关系吗?此外,在事前调整子实体是否正确,这是一种可接受的做法吗?不幸的是,这没有任何区别。我认为我的问题在于试图更改子实体的所有者,直接设置所有者或对注册为“主要”实体的“帐户”使用AssignRequest work fine,但当我尝试更改子实体的所有者时,它似乎被忽略了。