如何在帐户更新联系时获取联系人的引用ID也应该更新

时间:2016-10-06 06:26:49

标签: c# dynamics-crm

 if (entity.LogicalName == "account")
            {
                Entity Account = (Entity)service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, new ColumnSet(true));
                tracingService.Trace("1");
                Guid ContactGuid = Account.Id;
                tracingService.Trace("9");
                //throw new Exception("Contact guid" + ContactGuid);
                Entity objContact = (Entity)service.Retrieve("contact", ((EntityReference)Account.Attributes["new_account"]).Id, new ColumnSet(true));
                tracingService.Trace("2");
                string Updatename = Account.GetAttributeValue<string>("name");
                tracingService.Trace("3");
                objContact.Attributes["fullname"] = Updatename;
                tracingService.Trace("4");
                string Updateemail = Account.GetAttributeValue<string>("emailaddress1");
                tracingService.Trace("5");
                objContact.Attributes["emailaddress1"] = Updateemail;
                tracingService.Trace("6");
                string Updateaddress = Account.GetAttributeValue<string>("address1_composite");
                tracingService.Trace("7");
                objContact.Attributes["address1_composite"] = Updateaddress;

                service.Update(objContact);
            }

1 个答案:

答案 0 :(得分:0)

假设您想获取帐户主要联系人的ID:

EntityReference primaryContact = Account.GetAttributeValue<EntityReference>("primarycontactid");
Guid contactGuid = primaryContact.Id;

请注意,帐户的所有联系人的关系contact_customer_accounts为1:N,因此您需要从关系中检索它们并循环它们。