检查CRM 2011中的同一请求中是否存在多条记录

时间:2014-05-19 08:05:08

标签: c# dynamics-crm-2011

我需要将数据从SQL数据库迁移到CRM 2011。 我已经运行了第一次传递,只设置了SQL数据库中的必需字段和标识符。 现在我应该检索实体并使用查找更新值。

我正在使用批量操作(ExecuteMultipleRequest)执行插入和更新。 有没有类似的方法来检查是否已经创建了一个实体集合,如果是这种情况,可能会检索它们的id?

单一实体检查确实减慢了速度。

这是我用来检查记录是否存在的方法:

/// <summary>
/// Check if the entity with the specified value on the specified field exists
/// </summary>
/// <param name="entityName">Name of the entity</param>
/// /// <param name="field">Field to be checked</param>
/// <param name="fieldValue">Value to be checked</param>
/// <returns>The GUID of the existing return if it exists, an empty one otherwise</returns>

public Guid CheckIfExisting(string entityName, string field, string fieldValue)
{
    try
    {
        QueryExpression entityQuery = new QueryExpression(entityName);
        entityQuery.ColumnSet = new ColumnSet(entityName + "id");
        entityQuery.Criteria = new FilterExpression();
        entityQuery.Criteria.AddCondition(field, ConditionOperator.Equal, fieldValue);

        EntityCollection retrievedEntities = service.RetrieveMultiple(entityQuery);
        if (retrievedEntities.Entities.Count >0 && retrievedEntities.Entities[0].Id != null)
            return retrievedEntities.Entities[0].Id;
        else
            return Guid.Empty;
    }
    catch (Exception e)
    {
        Console.WriteLine("There was an exception of type" + e.GetType());
        Console.WriteLine("Error message " + e.Message);
        Console.WriteLine(e.StackTrace);

    }
    return Guid.Empty;
}

1 个答案:

答案 0 :(得分:0)

我已经为CRM做过许多类型的集成工作。有两种主要的方式来实现这个

最简单的方法是使用优秀的kingswaysoft SSIS组件进行CRM http://www.kingswaysoft.com/products/ssis-integration-toolkit-for-microsoft-dynamics-crm。这很好,因为它会给你一个&#34; upsert&#34;开箱即用的选项,允许您这样做。但它确实遇到了使用CRM检查每条记录的相同问题。

第二种更复杂但性能更好的方法是使用SSIS为您比较一些东西。基本前提是获取两个列表,一个用于源,一个用于目标,并使用完全外连接加入它们。使用条件拆分你的逻辑就是这个

  • 如果它在源中但不在目的地,请插入
  • 如果在目的地但不在源中,请删除
  • 如果两者都更新

当性能是一个因素时,这适用于大量数据。