2015-02-09 31 views
1

下面的代碼在的WriteLine拋出一個KeyNotFoundException():動態CRM 2015年在線:將無法獲取自定義字段

CrmConnection crmConnection = CrmConnection.Parse(_connectionString); 
using (OrganizationService service = new OrganizationService(crmConnection)) 
{ 
    QueryExpression query = new QueryExpression 
    { 
     EntityName = "contact", 
     ColumnSet = new ColumnSet("fullname", "new_customer_num", "new_is_customer"), 
    }; 

    EntityCollection contacts = service.RetrieveMultiple(query); 
    if (contacts.Entities.Count > 0) 
    { 
     foreach (var e in contacts.Entities) 
     { 
      System.Diagnostics.Debug.WriteLine(
       e.Attributes["fullname"].ToString() + "; " + 
       e.Attributes["new_is_customer"].ToString()); 
     } 
    } 
} 

我已經添加new_customer_num和new_is_customer(需要)字段聯繫人實體。如果我故意拼錯它們,則FaultException拋出service.RetrieveMultiple(query);所以我猜CRM「知道」我的自定義字段。那爲什麼不查詢結果包含它們呢?

回答

0

好,使其工作,我不得不添加一個條件:對於ColumnSet

QueryExpression query = new QueryExpression 
{ 
    EntityName = "contact", 
    ColumnSet = new ColumnSet("fullname", "ht_customer_num", "ht_is_customer"), 
    Criteria = new FilterExpression 
    { 
     Conditions = 
     { 
      new ConditionExpression 
      { 
       AttributeName = "new_is_customer", 
       Operator = ConditionOperator.Equal, 
       Values = { true } 
      }, 
     }, 
    } 
}; 

SDK文檔說,查詢將返回唯一非空值。

+0

你能告訴我你爲什麼用new_替換ht_嗎?我嘗試這個,但它不適合我 – 2017-06-15 10:23:43