Import Multiple Records Into CRM

我是研究僧i 提交于 2019-12-24 01:08:15

问题


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.


回答1:


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

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

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



来源:https://stackoverflow.com/questions/18229307/import-multiple-records-into-crm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!