March 21, 2013

Retrieve fault entity IDs when performing ExecuteMultiple

One of the new features introduced inside CRM 2011 Update Rollup 12 is the ExecuteMultipleRequest message in order to perform bulk data operations. More information can be found at this address:

Use ExecuteMultiple to Improve Performance for Bulk Data Load

When the ContinueOnError property is set to true the service will not stop if there are faults, but how we can retrieve the fault entity IDs?

We need to rely on the RequestIndex property inside ExecuteMultipleResponseItem object, the RequestIndex indicates the corresponding request.
ExecuteMultipleRequest requestWithResults = new ExecuteMultipleRequest()
{
    // assign settings that define execution behavior: continue on error, return responses 
    Settings = new ExecuteMultipleSettings()
    {
        ContinueOnError = true,
        ReturnResponses = true
    },
    // create an empty organization request collection
    Requests = new OrganizationRequestCollection()
};

// entities to be updated
foreach (var entity in entities)
{
    UpdateRequest updateRequest = new UpdateRequest { Target = entity };
    updateRequest.RequestId = entity.Id;
    requestWithResults.Requests.Add(updateRequest);
}

// execute the bulk update
ExecuteMultipleResponse responseWithResults = (ExecuteMultipleResponse)service.Execute(requestWithResults);

// in this variable we want to store the fault IDs
List<Guid> faultIDs = new List<Guid>();

foreach (ExecuteMultipleResponseItem responseItem in responseWithResults.Responses)
{
    // check if the response is fault
    if (responseItem.Fault != null)
    {
        // retrieve the request index
        int faultIndex = responseItem.RequestIndex;
        // we retrieve the fault ID from the Request collection using the fault index 
        Guid faultID = requestWithResults.Requests[faultIndex].RequestId.GetValueOrDefault(Guid.Empty);
        // store the value
        faultIDs.Add(faultID);
    }
}

2 comments:

  1. I have been trying to find different and fastest approach to load large amount of data from Oracle as a Source and CRM 2011 as destination. Currently we are using Scribe as the integration tool to load the data but as I have mentioned, I would like to know the faster approach. I would like to try the programmatic approach and use ExecuteMultipleRequest but i do not really have good insight on how to implement this in my scenario. Again! I have oracle as the source and I need to store the source Oracle data in CRM 2011 Price List Entity and Price List Item Entity. Can you please suggest me a step by step approach to this, so at least i can get started with something?

    Thanks,
    Durgesh

    ReplyDelete
  2. Hi Durgesh,
    I'm quite sure that Scribe implements the ExecuteMultipleRequest when it's connected to UR12+ instances. You can also try the SSIS Integration Toolkit from KingswaySoft, it also implement the ExecuteMultipleRequest.

    ReplyDelete