2

如何将数千条记录导入 CRM?
我有一个可能包含数千条记录的列表,并希望在 CRM 中创建记录。

目前,我有一个接收列表的方法调用另一个方法,该方法以 200 个批次创建记录,直到创建整个列表。
最初一次是 1000 个,因为这是 ExecuteMultipleRequest 的限制,但有时会超时。
增加 ExecuteMultipleRequest 的限制不是一种选择。

此方法传递整个列表

    /* Receives a list of Entity Records and executes an executemultiplerequest, Returns false if any fail */
    public bool CreateStagingRecords<T>(List<T> records) where T : Entity
    {
        try
        {
            return CreateStagingRecordsInBatches(records, 0, records.Count);
        }
        catch { }
        return false;
    }

此方法递归地导入小块列表

    /* int start is used both as the start point to process the list and as subImportNo **/
    private bool CreateStagingRecordsInBatches<T>(List<T> records, int start, int end, int tries=0) where T : Entity
    {
        var createAmount = (end - start > 200) ? 200 : end - start; // 200 or the difference between end and start
        var records200 = records.Skip(start).Take(createAmount);
        var multipleRequest = new ExecuteMultipleRequest
        {
            Settings = new ExecuteMultipleSettings { ContinueOnError = false, ReturnResponses = true },
            Requests = new OrganizationRequestCollection()
        };
        foreach (var staging in records200)
        {
            multipleRequest.Requests.Add(new CreateRequest { Target = staging });
        }
        ExecuteMultipleResponse responses = null;
        try
        {
            responses = (ExecuteMultipleResponse)service.Execute(multipleRequest); // this can timeout
        }
        catch (System.TimeoutException) // The request timed out
        {
            if (tries < 4) // re-try if this is not already been retried 5 times
                return this.CreateStagingRecordsInBatches(records, start, end, tries + 1);
            return false; // we have already tried too many times, abandon the entire import
        }
        foreach (var response in responses.Responses)
        {
            if (response == null || response.Fault != null) //error
            {
                if (tries < 4)
                    return this.CreateStagingRecordsInBatches(records, start, end, tries+1);
                return false; // response not good, rather than trying to recover, abandon this import
            }
        }
        if (createAmount == 200 && start + 200 < end) // if createAmount is less than 200 then everything has been created, and start+200 is not equal to or more than end
        {                
            return this.CreateStagingRecordsInBatches(records, start + 200, end); // create the next 200 or less stagings records with the sub-import number incremented
        }
        return true; // this should only happen if there are under 200 records
    }

上述方法有效,但由于这是一个常见问题,我想知道其他开发人员如何处理它。

4

1 回答 1

1

这是建议的方法之一,特别是如果您想要更严格的程序控制。

有关更多选项,请参阅此答案 如何将大量数据导入 CRM 2011 Online 实体?

我们以编程方式调用 CRM 导入过程并提交内存中生成的 CSV。

于 2013-08-15T12:36:48.737 回答