如何使用Dynamics CRM SDK搜索潜在客户或帐户
本文关键字:客户 搜索 何使用 Dynamics CRM SDK | 更新日期: 2023-09-27 18:00:36
有人能在CRM SDK中提供一个通过电子邮件检索线索的示例代码吗?有这样的内置函数吗?
Guid leadID = someLeadManager.GetByEmail(email);
假设您已经获得服务,则可以执行以下查询。
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);
}
是否要检索具有特定邮件的潜在客户?在这种情况下可以做这样的事情。
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中的一个示例。