如何使用Dynamics CRM SDK搜索潜在客户或帐户?

时间:2013-02-22 09:45:13

标签: c# dynamics-crm dynamics-crm-online

有人可以提供一个示例代码,用于通过CRM SDK中的电子邮件检索潜在客户吗?有没有像这样工作的内置函数?

Guid leadID = someLeadManager.GetByEmail(email);

2 个答案:

答案 0 :(得分:9)

假设您已获得服务,您可以执行以下查询。

private Guid GetGuidByEmail(String email)
{
  QueryExpression query = new QueryExpression
  {
    EntityName = "lead",
    ColumnSet = new ColumnSet("emailaddress1"),
    Criteria = new FilterExpression
    {
      Filters =
      {
        new FilterExpression
        {
          Conditions =
          {
            new ConditionExpression(
              "emailaddress1", ConditionOperator.Equals, email)
          }
        }
      }
    }
  };

  Entity entity = service.RetrieveMultiple(query).Entities.FirstOrDefault();
  if(entity != null)
    return entity.Id;
  return Guid.Empty;
}

现在,如果您需要过滤电子邮件的部分匹配,则查询会变短,相反,您可以使用这样的LINQ进行选择。

private IEnumerable<Guid> GetGuidsByEmail(String email)
{
  QueryExpression query = new QueryExpression
  {
    EntityName = "lead",
    ColumnSet = new ColumnSet("emailaddress1")
  };
  IEnumerable<Entity> entities = service.RetrieveMultiple(query).Entities;

  return entities
    .Where(element => element.Contains("emailaddress1"))
      .Where(element => Regex.IsMatch(element["emailaddress1"], email))
        .Select(element => element.Id);
}

答案 1 :(得分:4)

您想要检索具有特定邮件的潜在客户吗?在这种情况下,可以做这样的事情。

private EntityCollection GetLeadsWithEmail(
  IOrganizationService service, String wantedEmailAddress)
{
  QueryExpression query = new QueryExpression();
  query.EntityName = "lead";
  // the columns you want
  query.ColumnSet = new ColumnSet() { AllColumns = true };

  query.Criteria = new FilterExpression();
  query.Criteria.FilterOperator = LogicalOperator.And;
  query.Criteria.Conditions.Add(new ConditionExpression(
    "emailaddress1", ConditionOperator.Equal, wantedEmailAddress));
  return service.RetrieveMultiple(query);
}

这将检索在他们的emailaddress1字段中具有wantedEmailAddress的所有潜在客户。 然后,您可以检查是否有任何匹配来自您所在的位置;

EntityCollection leadCollection = GetLeadsWithEmail(
  service, "someone@example.com");
Entity entity = leadCollection[0];

您可能应首先使用leadCollection.Entities.Count检查集合中的实体数量,然后从那里继续。

来自MSDN的

Here's a sample