I'm building a console that connects to a D365 instance, finds records using fetch XML and updates those records with a specific value. It works, it is updating the records. However, it is only updating about one or two a second which is far from ideal, I have hundreds of thousands of records to update. I have seen / used console apps which update hundreds a second at least.
Am I going about what I want to do in the wrong way? There must be something in my code that is making it massively inefficient / stupid. I should also say that this is the first time I've used C# so it is possible I am doing something incredibly dumb.
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling.Connector;
namespace My.Crm.RetrieveMultipleConsole
{
class Program
{
static void Main(string[] args)
{
try
{
var connectionString = @"AuthType = Office365; Url = xxxx/;Username=xxxx;Password=xxxx";
CrmServiceClient conn = new CrmServiceClient(connectionString);
IOrganizationService service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
string fetchquery = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='account'>
<attribute name='name' />
<order attribute='name' descending='false' />
<filter type='and'>
<condition attribute='name' operator='like' value='%Tom%' />
</filter>
</entity>
</fetch>";
var multipleRequest = new Microsoft.Xrm.Sdk.Messages.ExecuteMultipleRequest()
{
Settings = new ExecuteMultipleSettings()
{
ContinueOnError = false,
ReturnResponses = true
},
Requests = new OrganizationRequestCollection()
};
EntityCollection accounts = service.RetrieveMultiple(new FetchExpression(fetchquery));
foreach (var c in accounts.Entities)
{
Console.WriteLine("accountid: {0}", c.Attributes["name"]);
Microsoft.Xrm.Sdk.Messages.UpdateRequest updateRequest = new Microsoft.Xrm.Sdk.Messages.UpdateRequest { Target = c };
multipleRequest.Requests.Add(updateRequest);
c.Attributes["name"] = "New Name";
Microsoft.Xrm.Sdk.Messages.ExecuteMultipleResponse multipleResponse = (Microsoft.Xrm.Sdk.Messages.ExecuteMultipleResponse)service.Execute(multipleRequest);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
}
}
Take out your service.Execute(multipleRequest)
from foreach
loop, keep it outside. That way the execution will be really ExecuteMultipleRequest
.
var multipleRequest = new Microsoft.Xrm.Sdk.Messages.ExecuteMultipleRequest()
{
Settings = new ExecuteMultipleSettings()
{
ContinueOnError = false,
ReturnResponses = true
},
Requests = new OrganizationRequestCollection()
};
EntityCollection accounts = service.RetrieveMultiple(new FetchExpression(fetchquery));
foreach (var c in accounts.Entities)
{
Console.WriteLine("accountid: {0}", c.Attributes["name"]);
Microsoft.Xrm.Sdk.Messages.UpdateRequest updateRequest = new Microsoft.Xrm.Sdk.Messages.UpdateRequest { Target = c };
multipleRequest.Requests.Add(updateRequest);
c.Attributes["name"] = "New Name";
}
Microsoft.Xrm.Sdk.Messages.ExecuteMultipleResponse multipleResponse = (Microsoft.Xrm.Sdk.Messages.ExecuteMultipleResponse)service.Execute(multipleRequest);