Using the Bulk API to Create Records

I wanted to build a data populator for Dynamics that created a bunch of custom records and did some associations between them.

(The reasoning is so I could be sure I was always validating the same thing).

To do this, I wanted to use the Bulk API.  The Bulk API has a limit of 1000 requests per message so in addition to bulk loading my requests, I needed to ensure that multiple requests could be queued up and handled by the server.

Here is some of the sample code I wrote for this interaction.

public void BulkDispatchCreateRequests<T>(List<T> requests)
 {
 int MaxBatchSize = 999;
 bool CreateBatchRequest = true;

ExecuteMultipleRequest requestWithResults = null;

for (int i = 0; i < requests.Count(); i++)
 {
 if (CreateBatchRequest)
 {
 requestWithResults = new ExecuteMultipleRequest()
 {
 // Assign settings that define execution behavior: continue on error, return responses. 
 Settings = new ExecuteMultipleSettings()
 {
 ContinueOnError = false,
 ReturnResponses = true
 },
 // Create an empty organization request collection.
 Requests = new OrganizationRequestCollection()
 };
 }
 
 requestWithResults.Requests.Add(requests[i] as OrganizationRequest);

if (requestWithResults.Requests.Count() == MaxBatchSize || requestWithResults.Requests.Count() == requests.Count())
 {
 try
 {
 ExecuteMultipleResponse responseWithResults = (ExecuteMultipleResponse)_service.Execute(requestWithResults);
 CreateBatchRequest = true;
 System.Diagnostics.Debug.WriteLine("Bulk Accessor Request Sent.");
 }
 catch (Exception ex)
 {
 System.Diagnostics.Debug.WriteLine("Could not bulk create Accessor - error: " + ex.ToString());
 }
 }
 else
 {
 CreateBatchRequest = false;
 }
 }
 }

I used 999 as my max batch size to proactively stay under the 1,000 item limit.

The big piece of code here is that all my record Create requests are batched and associated to a ExecuteMultipleRequest. Once this is all done you bundle into the Execute request and send them off.

I got a bit fancy in this implementation and created a generic function called BulkDispatchCreateRequests which allows me drop in Associate, Delete, Disassociate requests that can all be bulk handled and loaded without having to rewrite the function.

The implementation then becomes pretty simple in other areas of my code.

BulkDispatchCreateRequests<CreateRequest>(securityRequests);

 

Post A Reply

%d bloggers like this: