【发布时间】:2017-07-21 20:12:14
【问题描述】:
我不仅是 C# 的新手,而且是 Microsoft Dynamics 的新手,我很难通过插件访问我的联系人的父实体数据。我的目标是使用父帐户地址自动填充我的联系人地址,如果联系人地址字段为空。我觉得这是一个相对简单的过程,我把它复杂化了。
public void SetContactAddress()
{
// TODO: Go and get the contact's parent account record and retrieve the
// address fields. Then update the contact's address with the fields from the
// values from the account. Do not overrride the contact's address if one is provided/exists
var item = _baseModel.TargetEntity as Entity;
var parent = _baseModel.TargetEntity as Entity;
Contact contact = new Contact(); //New blank contact in memory
contact.Id = item.Id; //assign Id to the new local contact
var service = _targetContact as IOrganizationService;
Guid parentId = ((EntityReference)contact["new_parentid"]).Id;
Entity parentEntity = service.Retrieve("new_parent", parentId, new ColumnSet(true)); //retrieve parent entity
string[] addressAtrributes = {"Address1_City","Address1_Country","Address1_Line1","Address1_Line2",
"Address1_Line3", "Address1_PostalCode","Address1_StateOrProvince"};
foreach (var key in parentEntity.Attributes.Keys)
{
if (addressAtrributes.Contains(key))
{
//contact.Address1_City = key;
contact[key] = key;
}
}
//contact.Address1_City = "My City"; //I can easily hard code a city in
_baseModel.OrganizationService.Update(contact);
}
【问题讨论】:
-
你遇到了什么错误?
-
从业务角度来看,您的逻辑是错误的,因为如果子地址只有第一行而父地址完全不同并且包含所有三行,那么您的孩子将拥有自己的地址和父地址的两行,这将没有意义。仅当所有字段为空时才应复制此字段,否则您将要求系统数据发生灾难...
-
我以为这是通过映射自动完成的?例如,创建您的帐户并设置地址字段。然后为此帐户创建联系人,并应填充地址字段。这仅适用于创建,不适用于更新。那么在这种情况下,我会使用工作流程,而不是插件
标签: c# plugins dynamics-crm crm microsoft-dynamics