I have a few thousand records that needs to be updated after certain condition is met. So I have two functions, one that takes in an array of records and loops through them to update one by one and another one that tries to update in bulk in batches. Although the looping works, the batch doesn't.
The function that takes in an array of records and loops through them to update one by one that works fine is below:
public static async Task Updateinloop(string url, string accesskey, JArray records, string custId, string displayName)
{
try
{
using (HttpClient httpClient = new HttpClient())
{
// Set the base URL for the Dynamics 365 Web API
httpClient.BaseAddress = new Uri(url);
// Set the access token in the request headers
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accesskey);
foreach (JObject record in records)
{
string donationId = (string)record["new_donationid"];
System.Diagnostics.Debug.WriteLine("donationId: " + donationId);
// Construct the update request payload
JObject payload = new JObject();
payload[fieldName1] = true;
payload[fieldName2] = displayName;
payload[fieldName3] = DateTime.Now;
// Construct the update request URL
string updateUrl = $"api/data/v9.0/new_donations({donationId})";
// Send the update request
HttpResponseMessage response = await httpClient.PatchAsync(updateUrl, new StringContent(payload.ToString(), Encoding.UTF8, "application/json"));
// Process the response
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Donation updated successfully");
}
else
{
string errorMessage = await response.Content.ReadAsStringAsync();
Console.WriteLine("Error updating donation: " + errorMessage);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
But I am trying to do it in batches instead of looping it. So I have another function which is similar but modified code as below:
public static async Task UpdateBulk(string url, string accesskey, JArray records, string custId, string displayName)
{
try
{
using (HttpClient httpClient = new HttpClient())
{
// Set the base URL for the Dynamics 365 Web API
httpClient.BaseAddress = new Uri(url);
// Set the access token in the request headers
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accesskey);
// Create the batch request
MultipartContent batchContent = new MultipartContent("mixed", $"batch_{Guid.NewGuid()}");
// Set the Content-Type header for the batch request
batchContent.Headers.Remove("Content-Type");
batchContent.Headers.TryAddWithoutValidation("Content-Type", "multipart/mixed;boundary=batch_" + Guid.NewGuid().ToString());
int contentId = 1; // Track the content ID for each individual request
foreach (JObject record in records)
{
string donationId = (string)record["new_donationid"];
System.Diagnostics.Debug.WriteLine("donationId: " + donationId);
// Construct the update request payload
JObject payload = new JObject();
payload[fieldName1] = true;
payload[fieldName2] = displayName;
payload[fieldName3] = DateTime.Now;
// Construct the update request URL
string updateUrl = $"api/data/v9.0/new_donations({donationId})";
// Create the individual request content
StringContent requestContent = new StringContent(payload.ToString(), Encoding.UTF8, "application/json");
requestContent.Headers.Add("Content-ID", contentId.ToString());
requestContent.Headers.TryAddWithoutValidation("Content-Type", "application/http");
requestContent.Headers.TryAddWithoutValidation("Content-Transfer-Encoding", "binary");
contentId++;
// Add the individual request to the batch
batchContent.Add(requestContent);
}
// Create the batch request URL
string batchUrl = "api/data/v9.0/$batch";
// Send the batch update request
HttpResponseMessage response = await httpClient.PostAsync(batchUrl, batchContent);
// Process the response
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Donation batch updated successfully");
}
else
{
string errorMessage = await response.Content.ReadAsStringAsync();
Console.WriteLine("Error updating donations in batch: " + errorMessage);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Now it displays
Donation batch updated successfully
in the console, but the update does not happen. I have tried playing with some adjustments in code like changing Content-Type etc. but so far, I am not able to make it work in batches. The looping works fine.
Please help and advice. Thanks.