Skip to content

Commit

Permalink
Merge pull request #205 from rappen/bulkoperations-#195
Browse files Browse the repository at this point in the history
v0.1 of bulk operations #195
  • Loading branch information
rappen authored Jan 6, 2024
2 parents 8bd7429 + fcaf124 commit 4db5e81
Show file tree
Hide file tree
Showing 7 changed files with 547 additions and 362 deletions.
62 changes: 62 additions & 0 deletions BulkDataUpdater/AppCode/BDUGeneral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,67 @@ private void RetrieveRecordsReady()
InitializeTab();
}

private void SetIsMultiMessageAvailable()
{
if (string.IsNullOrEmpty(job?.Entity))
{
chkMultipleRequest.Enabled = false;
}
var message = string.Empty;
if (tabControl1.SelectedTab == tabUpdate)
{
message = "Update";
}
else if (tabControl1.SelectedTab == tabAssign)
{
message = "Update";
}
else if (tabControl1.SelectedTab == tabDelete)
{
message = "Delete";
}
if (string.IsNullOrEmpty(message))
{
chkMultipleRequest.Enabled = false;
ClearIfDisabled(chkMultipleRequest);
}
else
{
WorkAsync(new WorkAsyncInfo
{
Work = (w, a) =>
{
a.Result = Service.IsMessageAvailable(job.Entity, message + "Multiple", w);
},
ProgressChanged = (a) =>
{
SetWorkingMessage(a.UserState.ToString());
},
PostWorkCallBack = (a) =>
{
ShowErrorDialog(a.Error);
if (a.Error == null && a.Result is bool result)
{
chkMultipleRequest.Enabled = result;
}
else
{
chkMultipleRequest.Enabled = false;
}
ClearIfDisabled(chkMultipleRequest);
}
});
}
}

private static void ClearIfDisabled(CheckBox checker)
{
if (!checker.Enabled && checker.Checked)
{
checker.Checked = false;
}
}

private IEnumerable<Entity> GetIncludedRecords()
{
// The following line can be restored when xrmtb controls version > 2.2.6 is used
Expand Down Expand Up @@ -308,6 +369,7 @@ private JobExecuteOptions GetExecuteOptions()
{
DelayCallTime = int.TryParse(cmbDelayCall.Text, out var delay) ? delay : 0,
BatchSize = int.TryParse(cmbBatchSize.Text, out int updsize) ? updsize : 1,
MultipleRquest = chkMultipleRequest.Checked,
IgnoreErrors = chkIgnoreErrors.Checked,
BypassCustom = chkBypassPlugins.Checked
};
Expand Down
1 change: 1 addition & 0 deletions BulkDataUpdater/AppCode/BDUJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public class JobExecuteOptions
{
public int DelayCallTime { get; set; } = 0;
public int BatchSize { get; set; } = 1;
public bool MultipleRquest { get; set; } = false;
public bool IgnoreErrors { get; set; } = false;
public bool BypassCustom { get; set; } = false;
}
Expand Down
49 changes: 34 additions & 15 deletions BulkDataUpdater/AppCode/BDUUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private void UpdateRecords()
tsbCancel.Enabled = true;
splitContainer1.Enabled = false;
var selectedattributes = lvAttributes.Items.Cast<ListViewItem>().Select(i => i.Tag as BulkActionItem).ToList();
var entity = records.EntityName;
var entityname = records.EntityName;
var includedrecords = GetIncludedRecords();
working = true;
var executeoptions = GetExecuteOptions();
Expand All @@ -55,10 +55,9 @@ private void UpdateRecords()
var updated = 0;
var failed = 0;
var attributes = workargs.Argument as List<BulkActionItem>;
var batch = new ExecuteMultipleRequest
var entities = new EntityCollection
{
Settings = new ExecuteMultipleSettings { ContinueOnError = executeoptions.IgnoreErrors },
Requests = new OrganizationRequestCollection()
EntityName = entityname
};
foreach (var record in includedrecords)
{
Expand Down Expand Up @@ -97,34 +96,53 @@ private void UpdateRecords()
{
//if ((bai.DontTouch || bai.Action == BulkActionAction.Touch) && !attributesexists)
bgworker.ReportProgress(pct, "Reloading record " + current.ToString());
LoadMissingAttributesForRecord(record, entity, attributes);
LoadMissingAttributesForRecord(record, entityname, attributes);
}
try
{
if (GetUpdateRecord(record, attributes, current) is Entity updateentity && updateentity.Attributes.Count > 0)
{
var request = new UpdateRequest { Target = updateentity };
SetBypassPlugins(request, executeoptions.BypassCustom);
if (executeoptions.BatchSize == 1)
{
var request = new UpdateRequest { Target = updateentity };
SetBypassPlugins(request, executeoptions.BypassCustom);
bgworker.ReportProgress(pct, $"Updating record {current} of {total}");
Service.Execute(request);
updated++;
waitnow = true;
}
else
{
batch.Requests.Add(request);
if (batch.Requests.Count == executeoptions.BatchSize || current == total)
entities.Entities.Add(updateentity);
if (entities.Entities.Count == executeoptions.BatchSize || current == total)
{
bgworker.ReportProgress(pct, $"Updating records {current - batch.Requests.Count + 1}-{current} of {total}");
var response = Service.Execute(batch) as ExecuteMultipleResponse;
if (!executeoptions.IgnoreErrors && response?.IsFaulted == true)
bgworker.ReportProgress(pct, $"Updating records {current - entities.Entities.Count + 1}-{current} of {total}");
if (executeoptions.MultipleRquest)
{
throw new FaultException<OrganizationServiceFault>(response.Responses.FirstOrDefault(r => r.Fault != null).Fault);
var multireq = new UpdateMultipleRequest
{
// ConcurrencyBehavior = ConcurrencyBehavior.AlwaysOverwrite,
Targets = entities
};
var response = Service.Execute(multireq);
}
updated += batch.Requests.Count;
batch.Requests.Clear();
else
{
var batch = new ExecuteMultipleRequest
{
Settings = new ExecuteMultipleSettings { ContinueOnError = executeoptions.IgnoreErrors },
Requests = new OrganizationRequestCollection()
};
foreach (var entity in entities.Entities)
{
var req = new UpdateRequest { Target = entity };
SetBypassPlugins(req, executeoptions.BypassCustom);
batch.Requests.Add(req);
}
Service.Execute(batch);
}
updated += entities.Entities.Count;
entities.Entities.Clear();
waitnow = true;
}
}
Expand Down Expand Up @@ -491,6 +509,7 @@ private void SetUpdateFromJob(JobUpdate job)
{
cmbDelayCall.SelectedItem = cmbDelayCall.Items.Cast<string>().FirstOrDefault(i => i == job.ExecuteOptions.DelayCallTime.ToString());
cmbBatchSize.SelectedItem = cmbBatchSize.Items.Cast<string>().FirstOrDefault(i => i == job.ExecuteOptions.BatchSize.ToString());
chkMultipleRequest.Checked = job.ExecuteOptions.MultipleRquest;
chkIgnoreErrors.Checked = job.ExecuteOptions.IgnoreErrors;
chkBypassPlugins.Checked = job.ExecuteOptions.BypassCustom;
lvAttributes.Items.Clear();
Expand Down
Loading

0 comments on commit 4db5e81

Please sign in to comment.