'How to decrease the execution time of multiple requests in Dynamics 365

I'm in big trouble. The execution time of my multiple requests in the code is being longer than the azure app timeout. I need to update a lot of records and in the end return some data to the web site in azure. I'm sending batches of 200 requests to update 200 records. I need a faster way to batch update records.

My code:

public static Boolean BulkUpdateNoSorteado(CrmServiceClient service, EntityCollection entities)
{
    // Create an ExecuteMultipleRequest object.
    var multipleRequest = 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()
            };

    try
    {
        var countRequest = Int32.Parse(ConfigurationManager.AppSettings["requestCount"]);
                
        // Add a UpdateRequest for each entity to the request collection.
        foreach (var entity in entities.Entities)
        {
            SetStateRequest request = new SetStateRequest
                    {
                        EntityMoniker = new EntityReference(entity.LogicalName, entity.Id),
                        State = new OptionSetValue(1),
                        Status = new OptionSetValue((int)Domain.Enum.EnumStatusTicket.Nao_sorteado)
                    };
            multipleRequest.Requests.Add(request);

            if (multipleRequest.Requests.Count == countRequest || entity == entities.Entities.Last())
            {
                if (service.OrganizationServiceProxy == null)
                {
                    service = FactoryGetService.AccessTokenGeneratorAsync();
                }

                ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)service.Execute(multipleRequest);

                multipleRequest = 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()
                        };
                }
       }

       return true;
    } 
    catch (Exception)
    {
         throw;
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source