2014-09-26 108 views
-1

我已經使用ASP.NET創建了聯繫人記錄。現在我需要檢查聯繫人記錄是否存在。如果存在,請更新相同的記錄。通過預先查找已經下載了FetchXML並添加到我的FetchXML變量中。請建議邏輯。以下是我的代碼。動態CRM-聯繫人實體的更新記錄

// 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 

回答

0

你的邏輯似乎沒問題,除了FectchXML查詢。你有你的代碼的方式,你總是最終更新檢索到的第一個記錄,其手機字段已填充。這似乎不是檢查聯繫人是否已存在的好方法。

我建議你改變你的提取過濾器。在過濾條件中,您必須使用表示所有聯繫人唯一性的屬性。

除此之外,您的代碼看起來不錯。

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獲得的記錄。

其餘的代碼沒問題(我已經格式化了一下,使問題更具可讀性)。