3

I am using the Execute Multiple Response class to pass in an Entity Collection for Upsert into Dynamics CRM, I would like to know how to add the failed entity names to a collection and then use exception handling on each of these entities.

Cœur
  • 37,241
  • 25
  • 195
  • 267
John Abel
  • 271
  • 6
  • 20

1 Answers1

5

Assuming you create your ExecuteMultipleRequest something like this:

var entityCollection; // your EntityCollection
var requests = new ExecuteMultipleRequest();

foreach (var entity in entityCollection.Entities) {
    var upsertRequest = new UpsertRequest { Target = entity };
    requests.Requests.Add(upsertRequest);
}

You should be able to execute the request, iterate through the responses and for each determine whether a fault occurred:

var responses = service.Execute(requests);
var errors = new List<Entity>();

foreach (var response in responses.Responses) {
    if (response.Fault != null) {
        var entity = entityCollection[response.RequestIndex];
        errors.Add(entity);
    }
}

response.RequestIndex is used to access the response's corresponding request by matching their indexes.

responses.Responses contains a collection of ExecuteMultipleResponseItem. Documentation on their properties can be found here.

Dave Clark
  • 2,243
  • 15
  • 32
  • 1
    Thanks for the response Dave, helps a lot!! A couple of follow up questions. requests[response.RequestIndex] is the index of the ExecuteMultipleRequest , how would you get the entity from the index? My errors.Add(entity) gives me "cannot convert from 'object' to 'Microsoft.Xrm.Sdk.Entity' – John Abel Mar 24 '17 at 23:35
  • My mistake, you should get your entity from the `EntityCollection`, not from the `ExecuteMultipleRequest.Requests`. Therefore the line would read: `var entity = entityCollection[response.RequestIndex];` – Dave Clark Mar 27 '17 at 07:26