我正在开展一个项目,该项目要求将xlsx文件中的用户输入数据与Microsoft Dynamics CRM进行匹配。
我有一个执行匹配的方法。
public EntityViewModel Match(EntityViewModel inputEntity)
{
try
{
// Connect to the Organization service.
// The using statement assures that the service proxy will be properly disposed.
using (var serviceProxy = CrmServiceFactory.CreateNew())
{
if (!string.IsNullOrEmpty(inputEntity.ClientName))
CrmHelper.ClientMatch(serviceProxy, inputEntity);
var product = false;
if ((string.IsNullOrEmpty(inputEntity.Category) || inputEntity.Category != revenuecategoryos.Insurance.ToString())
&& !string.IsNullOrEmpty(inputEntity.ProductName) && !string.IsNullOrEmpty(inputEntity.ProductNumber) && inputEntity.ClientId != Guid.Empty)
{
product = CrmHelper.ClientAssetMatch(serviceProxy, inputEntity);
}
if (((string.IsNullOrEmpty(inputEntity.Category) && !product) || inputEntity.Category == revenuecategoryos.Insurance.ToString()) && !string.IsNullOrEmpty(inputEntity.ProductNumber) && !string.IsNullOrEmpty(inputEntity.ProductName))
{
CrmHelper.InsuranceMatch(serviceProxy, inputEntity);
}
if (!string.IsNullOrEmpty(inputEntity.ProductProvider) && !string.IsNullOrEmpty(inputEntity.ProductNumber) && !string.IsNullOrEmpty(inputEntity.ProductName))
{
CrmHelper.ProviderMatch(serviceProxy, inputEntity);
}
if (inputEntity.Type == revenuetypeos.Upfront.ToString() && !string.IsNullOrEmpty(inputEntity.Opportunity))
{
CrmHelper.OpportunityMatch(serviceProxy, inputEntity);
}
return inputEntity;
}
}
catch (Exception ex)
{
//handle the exception
}
}
如您所见,它调用了5种方法来执行匹配。他们查询5个不同的独立实体。
internal static bool ClientMatch(IOrganizationService crm, EntityViewModel inputEntity)
{
#region Using Retrieve Multiple
// Create a column set holding the names of the columns to be retrieved.
var cols = new ColumnSet("fullname");
// Create the query.
var query = new QueryExpression
{
EntityName = Xrm.Contact.EntityLogicalName,
ColumnSet = cols
};
query.Criteria.AddCondition("fullname", ConditionOperator.Equal, inputEntity.ClientName);
query.Criteria.AddCondition("statecode", ConditionOperator.Equal, (int)ContactState.Active);
// Create the request object.
var clientList = crm.RetrieveMultiple(query);
// If there's not only one existing record in CRM with this key value
if (recordList == null || recordList.Entities == null || recordList.Entities.Count != 1)
{
// if we couldn't find only one record with same key value (maybe none, maybe multiple records)
query.Criteria.Conditions.Clear();
query.Criteria.AddCondition("importmetadata", ConditionOperator.Like,
string.Format("%{0}, {1}, {2}%", inputEntity.ProductName, inputEntity.ProductNumber, inputEntity.ProductProvider));
query.Criteria.AddCondition("statecode", ConditionOperator.Equal, (int)ContactState.Active);
recordList = crm.RetrieveMultiple(query);
}
if (recordList != null && recordList.Entities != null && recordList.Entities.Count == 1)
{
var client = recordList.Entities[0];
inputEntity.ClientId = client.Id;
inputEntity.ClientName = client.Attributes.Contains("fullname") ? client.Attributes["fullname"].ToString() : string.Empty;
return true;
}
return false;
#endregion Using Retrieve Multiple
}
internal static bool ClientAssetMatch(IOrganizationService crm, EntityViewModel inputEntity)
{
#region Using Retrieve Multiple
// Create a column set holding the names of the columns to be retrieved.
var cols = new ColumnSet(new[] { "assetname", "accountnumber", "revenuecategory" });
// Build the filter based on the condition.
var filter = new FilterExpression
{
FilterOperator = LogicalOperator.And
};
filter.AddCondition("contactid", ConditionOperator.Equal, inputEntity.ClientId);
// Create a LinkEntity to link the owner's information to the account.
var link = new LinkEntity
{
LinkCriteria = filter,
LinkFromEntityName = clientasset.EntityLogicalName,
LinkFromAttributeName = "primaryclient",
LinkToEntityName = Xrm.Contact.EntityLogicalName,
LinkToAttributeName = "contactid"
};
// Create the query.
var query = new QueryExpression
{
EntityName = clientasset.EntityLogicalName,
ColumnSet = cols
};
query.LinkEntities.Add(link);
query.Criteria.AddCondition("statecode", ConditionOperator.Equal, (int)clientassetState.Active);
query.Criteria.AddCondition("assetname", ConditionOperator.Equal, inputEntity.ProductName);
query.Criteria.AddCondition("accountnumber", ConditionOperator.Equal, inputEntity.ProductNumber);
// Create the request object.
var recordList = crm.RetrieveMultiple(query);
if (recordList == null || recordList.Entities == null || recordList.Entities.Count != 1)
{
// if we couldn't find only one record with same key value (maybe none, maybe multiple records)
query.Criteria.Conditions.Clear();
query.Criteria.AddCondition("importmetadata", ConditionOperator.Like,
string.Format("%{0}, {1}%", inputEntity.ProductName, inputEntity.ProductNumber));
query.Criteria.AddCondition("statecode", ConditionOperator.Equal, (int)clientassetState.Active);
recordList = crm.RetrieveMultiple(query);
}
if (recordList.Entities.Count == 1)
{
var client = recordList.Entities[0];
inputEntity.ProductId = client.Id;
inputEntity.ProductName = client.Attributes.Contains("assetname") ? client.Attributes["assetname"].ToString() : string.Empty;
inputEntity.ProductNumber = client.Attributes.Contains("accountnumber") ? client.Attributes["accountnumber"].ToString() : string.Empty;
inputEntity.IsClientAsset = true;
if (string.IsNullOrEmpty(inputEntity.Category) && client.Attributes.Contains("revenuecategory") && client.Attributes["revenuecategory"] != null)
inputEntity.Category = GetCategory(((OptionSetValue)client.Attributes["revenuecategory"]).Value);
return true;
}
return false;
#endregion Using Retrieve Multiple
}
internal static bool InsuranceMatch(IOrganizationService crm, EntityViewModel inputEntity)
{
#region Using Retrieve Multiple
// Create a column set holding the names of the columns to be retrieved.
var cols = new ColumnSet(new[] { "name", "policynumber" });
// Build the filter based on the condition.
var filter = new FilterExpression
{
FilterOperator = LogicalOperator.And
};
filter.AddCondition("contactid", ConditionOperator.Equal, inputEntity.ClientId);
// Create a LinkEntity to link the owner's information to the account.
var link = new LinkEntity
{
LinkCriteria = filter,
LinkFromEntityName = personalinsurance.EntityLogicalName,
LinkFromAttributeName = "individualowner",
LinkToEntityName = Xrm.Contact.EntityLogicalName,
LinkToAttributeName = "contactid"
};
// Create the query.
var query = new QueryExpression
{
EntityName = personalinsurance.EntityLogicalName,
ColumnSet = cols
};
query.LinkEntities.Add(link);
query.Criteria.AddCondition("statecode", ConditionOperator.Equal, (int)personalinsuranceState.Active);
query.Criteria.AddCondition("name", ConditionOperator.Equal, inputEntity.ProductName);
query.Criteria.AddCondition("policynumber", ConditionOperator.Equal, inputEntity.ProductNumber);
// Create the request object.
var recordList = crm.RetrieveMultiple(query);
if (recordList == null || recordList.Entities == null || recordList.Entities.Count != 1)
{
// if we couldn't find only one record with same key value (maybe none, maybe multiple records)
query.Criteria.Conditions.Clear();
query.Criteria.AddCondition("importmetadata", ConditionOperator.Like,
string.Format("%{0}, {1}%", inputEntity.ProductName, inputEntity.ProductNumber));
query.Criteria.AddCondition("statecode", ConditionOperator.Equal, (int)personalinsuranceState.Active);
recordList = crm.RetrieveMultiple(query);
}
if (recordList.Entities.Count == 1)
{
var client = recordList.Entities[0];
inputEntity.ProductId = client.Id;
inputEntity.ProductName = client.Attributes.Contains("name") ? client.Attributes["name"].ToString() : string.Empty;
inputEntity.ProductNumber = client.Attributes.Contains("policynumber") ? client.Attributes["policynumber"].ToString() : string.Empty;
inputEntity.IsClientAsset = false;
// If it's a personal insurance, it's always an Insurance Revenue Category type
inputEntity.Category = revenuecategoryos.Insurance.ToString();
return true;
}
return false;
#endregion Using Retrieve Multiple
}
internal static bool ProviderMatch(IOrganizationService crm, EntityViewModel inputEntity)
{
#region Using Retrieve Multiple
// Create a column set holding the names of the columns to be retrieved.
var cols = new ColumnSet("name");
// Create the query.
var query = new QueryExpression
{
EntityName = Account.EntityLogicalName,
ColumnSet = cols
};
query.Criteria.AddCondition("name", ConditionOperator.Equal, inputEntity.ProductProvider);
query.Criteria.AddCondition("statecode", ConditionOperator.Equal, (int)AccountState.Active);
// Create the request object.
var recordList = crm.RetrieveMultiple(query);
// If there's not only one existing record in CRM with this key value
if (recordList == null || recordList.Entities == null || recordList.Entities.Count != 1)
{
// if we couldn't find only one record with same key value (maybe none, maybe multiple records)
query.Criteria.Conditions.Clear();
query.Criteria.AddCondition("importmetadata", ConditionOperator.Like, "%" + inputEntity.ProductProvider + "%");
query.Criteria.AddCondition("statecode", ConditionOperator.Equal, (int)AccountState.Active);
recordList = crm.RetrieveMultiple(query);
}
if (recordList != null && recordList.Entities != null && recordList.Entities.Count == 1)
{
var client = recordList.Entities[0];
inputEntity.ProductProviderId = client.Id;
inputEntity.ProductProvider = client.Attributes.Contains("name") ? client.Attributes["name"].ToString() : string.Empty;
return true;
}
return false;
#endregion Using Retrieve Multiple
}
internal static bool OpportunityMatch(IOrganizationService crm, EntityViewModel inputEntity)
{
#region Using Retrieve Multiple
// Create a column set holding the names of the columns to be retrieved.
var cols = new ColumnSet("name");
// Create the query.
var query = new QueryExpression
{
EntityName = Opportunity.EntityLogicalName,
ColumnSet = cols
};
query.Criteria.AddCondition("name", ConditionOperator.Equal, inputEntity.Opportunity);
query.Criteria.AddCondition("statecode", ConditionOperator.NotEqual, (int)OpportunityState.Lost);
// Create the request object.
var opportunityList = crm.RetrieveMultiple(query);
// If there's not only one existing record in CRM with this key value
if (opportunityList == null || opportunityList.Entities == null || opportunityList.Entities.Count != 1)
{
// if we couldn't find only one record with same key value (maybe none, maybe multiple records)
query.Criteria.Conditions.Clear();
query.Criteria.AddCondition("importmetadata", ConditionOperator.Like, "%" + inputEntity.Opportunity + "%");
query.Criteria.AddCondition("statecode", ConditionOperator.NotEqual, (int)OpportunityState.Lost);
opportunityList = crm.RetrieveMultiple(query);
}
if (recordList != null && recordList.Entities != null && recordList.Entities.Count == 1)
{
var opportunity = recordList.Entities[0];
inputEntity.OpportunityId = opportunity.Id;
inputEntity.Opportunity = opportunity.Attributes.Contains("name") ? opportunity.Attributes["name"].ToString() : string.Empty;
return true;
}
return false;
#endregion Using Retrieve Multiple
}
通常,此代码适用于少于15行的小型Excel文件。但是当用户输入更大的文件时,性能极其缓慢,特别是如果有2个或更多用户同时上传文件(因为整个匹配过程由队列触发的webjob管理)。我尝试实现多任务处理以同时匹配多个项目,但没有任何改变。
我一直在想,也许我需要的是找到一些方法将上面的5个函数合并在一起。但到目前为止,我似乎无法找到任何办法。 接口IOrganizationService的RetrieveMultiple方法似乎一次只能处理一个QueryExpression对象。上面列出的5个实体是分开的。我的问题是可以在一次crm调用中查询它们吗?如果是这样,怎么样?如果没有,还有其他方法可以改善性能吗?我们的客户当然不想花费超过10分钟等待匹配过程。他们需要更快的东西。感谢。
答案 0 :(得分:1)
是的,您可以尝试使用ExecuteMultipleRequest类来查看是否可以提高性能。
以下是为一个多请求发送四个帐户的RetrieveMultipleRequests和四个联系人的示例,并打印结果的名称。您需要对其进行调整以处理所需实体的查询,但查询五个单独的实体应该可以正常工作。
输出:
UPDATE AC_Property
SET NRI_1 = (WI_1 * .8)
WHERE COALESCE(WI_1,0) <> 0 and RSV_CAT = 'PDNP'