Dynamics CRM-联系实体的更新记录

时间:2014-09-26 09:33:26

标签: dynamics-crm-2013

我使用ASP.NET创建了联系人记录。现在我需要检查联系人记录是否存在。如果存在,请更新相同的记录。通过提前找到已经下载的FetchXML并添加到我的FetchXML变量中。请提出逻辑。以下是我的代码。

// Establish a connection to crm and get the connection proxy 

string connectionString = "xyz; Username= xyz ;Password=xyz";
CrmConnection connect = CrmConnection.Parse(connectionString);
OrganizationService service;


using (service = new OrganizationService(connect))
{
    WhoAmIRequest request = new WhoAmIRequest();
    Guid userId = ((WhoAmIResponse)service.Execute(request)).UserId;

    ContactDetails contact = new ContactDetails();
    //Check if the contact record exists . If exists , update the same record.
    //Fecthxml query 
    string fetchXml = @" <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                            <entity name='contact'>
                                <attribute name='fullname' />
                                <attribute name='parentcustomerid' />
                                <attribute name='telephone1' />
                                <attribute name='emailaddress1' />
                                <attribute name='contactid' />
                                <order attribute='fullname' descending='false' />
                                <filter type='and'>
                                    <condition attribute= 'mobilephone' operator='not-null' />
                                </filter>
                            </entity>
                        </fetch>" ;
    FetchExpression query = new FetchExpression(fetchXml);
    EntityCollection results = service.RetrieveMultiple(query);

    if (results.Entities.Count > 0)
    {
        Entity contactRecord = results[0];
        contactRecord["firstname"] = contactInfo.FirstName;
        contactRecord["lastname"] = contactInfo.LastName;
        contactRecord["emailaddress1"] = contactInfo.EmailId;
        contactRecord["mobilephone"] = contactInfo.MobilePhone;
        contactRecord["address1_line1"] = contactInfo.Street1;
        contactRecord["address1_line2"] = contactInfo.Street2;
        contactRecord["address1_line3"] = contactInfo.Street3;
        contactRecord["address1_city"] = contactInfo.City;
        service.Update(contactRecord);
    }
    //Else, Create the contact record
    else
    {
        Entity entity = new Entity();
        entity.LogicalName = "contact";

        entity["firstname"] = contactInfo.FirstName;
        entity["lastname"] = contactInfo.LastName;
        entity["emailaddress1"] = contactInfo.EmailId;
        entity["mobilephone"] = contactInfo.MobilePhone;
        entity["address1_line1"] = contactInfo.Street1;
        entity["address1_line2"] = contactInfo.Street2;
        entity["address1_line3"] = contactInfo.Street3;
        entity["address1_city"] = contactInfo.City;
        entity["address1_stateorprovince"] = contactInfo.State;
        entity["address1_country"] = contactInfo.Country;
        entity["spousesname"] = contactInfo.SpouseName;
        entity["birthdate"] = contactInfo.Birthday;
        entity["anniversary"] = contactInfo.Anniversary;

        //Create entity gender with option value
        if (contactInfo.Gender == "Male")
        {
            entity["gendercode"] = new OptionSetValue(1);
        }
        else
        {
            entity["gendercode"] = new OptionSetValue(2);
        }

        //entity["familystatuscode"] = contactInfo.MaritalStatus;

        if (contactInfo.MaritalStatus == "Single")
        {
            entity["familystatuscode"] = new OptionSetValue(1);
        }
        else
        {
            entity["familystatuscode"] = new OptionSetValue(2);
        }
        service.Create(entity);
    }
}
// Create the entity 

2 个答案:

答案 0 :(得分:0)

你的逻辑似乎没问题,但FectchXML查询除外。您拥有代码的方式总是会更新已检索到的已填充移动电话字段的第一条记录。这似乎不是检查联系人是否已存在的好方法。

我建议您更改提取的过滤器。在过滤条件中,您必须使用表示所有联系人唯一性的属性。

除此之外,您的代码看起来还不错。

答案 1 :(得分:0)

就像nunoalmeieda所说,你需要有一个更好的方法来确定联系人是否已经存在。识别联系人是否已存在的常用方法是检查电子邮件地址是否已存在,因为两个人不太可能拥有相同的电子邮件地址。

我已更新您的基本代码,以显示如何使用FetchXML完成。

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
  <entity name="contact">
      <attribute name='fullname' />
      <attribute name='parentcustomerid' />
      <attribute name='telephone1' />
      <attribute name='emailaddress1' />
      <attribute name='contactid' />
      <order attribute="fullname" descending="false" />
      <filter type="and">
          <condition attribute="emailaddress1" operator="eq" value=contactInfo.EmailId />
      </filter>
  </entity>
</fetch>

这里的逻辑是我正在检查emailaddress1(CRM的联系实体中的字段)的值是否等于您的contactInfo.EmailId的值。我假设contactInfo是从ASP.NET获得的记录。

你的其余代码很好(我已经格式化了一下以使问题更具可读性)。