如何将数千条记录导入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
}
上述方法有效,但由于这是一个常见问题,我想知道其他开发人员如何处理它。
答案 0 :(得分:1)
这是建议的方法之一,特别是如果你想要更严格的程序控制。
有关更多选项,请参阅此答案 How to import large volume of data into CRM 2011 Online entities?
我们以编程方式调用CRM导入过程并提交内存生成的CSV。