将多个记录导入CRM [英] Import Multiple Records Into CRM

查看:67
本文介绍了将多个记录导入CRM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将成千上万条记录导入CRM?


我有一个可能包含数千条记录的列表,并且想要在CRM中创建记录。




当前,我有一个接收列表的方法,该方法调用了另一种方法,该方法以200为批次创建记录,直到创建整个列表为止。


最初一次是1000,因为这是ExecuteMultipleRequest的限制,但是有时会超时。


不能增加ExecuteMultipleRequest的限制。




此方法传递整个列表

How do you import thousands of records into CRM?
I have a list with possible thousands of records and want to create the records in CRM.

Currently I have a method that receives the list calls another method that creates the records in batches of 200 until the whole list is created.
Originally it was 1000 at a time as this is the limit for ExecuteMultipleRequest, but that was timing out at times.
Increasing the limit for ExecuteMultipleRequest is not an option.

This method is passed the entire list

    /* 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;
    }

此方法以小块形式递归导入列表

This method recursively imports the list in small pieces

    /* 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
    }

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

The above approach works, but as this is a common problem I would like to know how other developers approach it.

推荐答案

这是建议的方法之一做到这一点的方法,尤其是如果您希望更严格的程序控制。

This is one of the suggested ways to do it, especially if you want tighter programatic control.

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

See this answer for more options How to import large volume of data into CRM 2011 Online entities?

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

We programatically invoke the CRM import process and submit an in-memory generated CSV.

这篇关于将多个记录导入CRM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆