bulk Create/Update/Delete for Dynamics 365

 Secret

Create / Update / Delete can be done with bulk operation, to avoid more DB hits and it will improve performance of the plugins and or code.

Here is reference MSDN URL http://msdn.microsoft.com/en-us/library/jj863631.aspx

Default max batch size is 1000, its applicable for all three create/update/delete.

  1. bulk Create can be done with following code snippet
CreateRequest createRequest = new CreateRequest { Target = 'your Entity Object' };

2. bulk Update can be done with following code snippet

UpdateRequest updateRequest = new UpdateRequest { Target = 'your Entity Object' };

3. bulk Delete can be done with following code snippet

DeleteRequest deleteRequest = new DeleteRequest { Target = 'your Entity Reference Object' };

Here are sample code blocks for bulk Update

 

Create multiple execute request

/// <summary>
 /// Create an ExecuteMultipleRequest object
 /// </summary>
 /// <returns></returns>
 private static ExecuteMultipleRequest CreateBulkExcuteRequest()
 {
 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()
 };
 return multipleRequest;
 }

 

  1. Create ExecuteMultipleRequest object 
/// <summary>
 /// Create bulk records 
 /// </summary>
 /// <param name="service"></param>
 /// <param name="entities"></param>
 public static void BulkCreate(IOrganizationService service, DataCollection<Entity> entities)
 {
 // Create an ExecuteMultipleRequest object.
 var multipleRequest = CreateBulkExcuteRequest();

// Add a CreateRequest for each entity to the request collection.
 foreach (var entity in entities)
 {
 CreateRequest createRequest = new CreateRequest { Target = entity };
 multipleRequest.Requests.Add(createRequest);
 }

// Execute all the requests in the request collection using a single web method call.
 ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)service.Execute(multipleRequest);

}

2. Bulk delete 
private static void DeleteQuoteLines(SoftContext context, Entity quote)
 {
 context.Trace("Begin remove Quotelines process");

EntityReference partialQuoteRef = (EntityReference)quote["soft_partioalquote"];

QueryExpression queryPQuoteline = new QueryExpression("soft_partionalquoteline");
 queryPQuoteline.Criteria.AddCondition("soft_partionalquote", ConditionOperator.Equal, partialQuoteRef.Id);

EntityCollection pQLines = context.OrgService.RetrieveMultiple(queryPQuoteline);
 context.Trace("Total Quotelines to process " + pQLines.Entities.Count);

// Create an ExecuteMultipleRequest object.
 var multipleRequest = CreateBulkExcuteRequest();

foreach (var pQline in pQLines.Entities)
 {
 EntityReference pqLineRef = new EntityReference(pQline.LogicalName, pQline.Id);

DeleteRequest deleteRequest = new DeleteRequest { Target = pqLineRef };
 multipleRequest.Requests.Add(deleteRequest);
 context.Trace("added to remove list: " + pQline.Id);
 }

// Execute all the requests in the request collection using a single web method call.
 ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)context.OrgService.Execute(multipleRequest);
 context.Trace("Executed remove list");
 }
3. Update/Create the bulk records 
/// <summary>
 /// update QLines
 /// </summary>
 /// <param name="context"></param>
 /// <param name="quote"></param>
 private static void UpdateQLines(SelectContext context, Entity quote)
 {
 context.Trace("update q-lines");
 QueryExpression queryQuoteline = new QueryExpression("quotedetail")
 {
 ColumnSet = new ColumnSet("lor_sentversion", "soft_quotelinesent")
 };

queryQuoteline.Criteria.AddCondition("quoteid", ConditionOperator.Equal, quote.Id);
 queryQuoteline.Criteria.AddCondition("lor_quotelinesent", ConditionOperator.Equal, false);

EntityCollection qLines = context.OrgService.RetrieveMultiple(queryQuoteline);
 context.Trace("Total Quotelines to process " + qLines.Entities.Count);

ExecuteMultipleRequest multipleRequest = CreateBulkExcuteRequest();

foreach (Entity qline in qLines.Entities)
 {
 context.Trace("Processing q-line: " + qline.Id);
 if (qline.Contains("soft_quotelinesent") && (bool)qline["soft_quotelinesent"] == false)
 {
 UpdateRequest createRequest = new UpdateRequest { Target = qline };
 // add the field to be updated
 qline.Attributes["soft_quotelinesent"] = true;
 qline.Attributes["soft_sentversion"] = (int)quote["soft_currentversion"];
 multipleRequest.Requests.Add(createRequest);
 context.Trace("q-line aaded to update: " + qline.Id);

}
 else
 context.Trace("q-line not added to update: " + qline.Id);
 }

// Execute all the requests in the request collection using a single web method call.
 ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)context.OrgService.Execute(multipleRequest);
 context.Trace("Executed Q-Line update.....");

}

 

Some more links here for same one.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s