2015-02-09 30 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