4

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

Guid leadID = someLeadManager.GetByEmail(email);
4

2 回答 2

9

假设您已经获得了service,您可以执行以下查询。

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);
}
于 2013-02-22T19:35:18.323 回答
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 字段中检索所有希望EmailAddress 的潜在客户。然后,您可以检查您调用它的位置是否有任何匹配项;

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

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

这是来自 MSDN的示例。

于 2013-02-22T10:16:45.430 回答