How to use CreateMultiple and UpdateMultiple messages using DataVerse SDK Client

Hello everyone and welcome to my blog. In today’s blog, I will discuss a preview feature recently release by Microsoft.

Through my blog I share interesting tips and discuss on latest releases of Microsoft.NET technologies, Dynamics 365 and Power Platform, SharePoint and Client scripting libraries. Please subscribe to my blog to stay updated.

Microsoft has announced two new messages in DataVerse SDK- CreateMultiple and UpdateMultiple. Below is the quote-unquote from Microsoft documentation regarding these messages. You can find more information here.

The CreateMultiple and UpdateMultiple messages are optimized for performance when performing multiple create or update operations on the same table. This optimization is useful in cases where you’re loading or updating records in bulk.

Unlike the ExecuteMultiple, this does not have the 1000 records per batch limitation. The entire operation is in transaction.

Please CreateMultiple and UpdateMultiple will trigger plugins registered on create and update of individual records. Apart from that, you can also register your plugins on CreateMultiple and UpdateMultiple messages. So if required, you can move the existing plugin logic on create or update of individual records to the corresponding CreateMultiple or UpdateMultiple message.

Well that’s all theory, lets test these new messages with some sample code. Before you start using these messages for a table, we need to make sure the message is supported for that table. Following is the code from Microsoft Learn to check if these messages are supported on a table.


public static bool IsMessageAvailable(
    IOrganizationService service,
    string entityLogicalName,
    string messageName)
{
    QueryExpression query = new("sdkmessagefilter")
    {
        ColumnSet = new ColumnSet("sdkmessagefilterid"),
        Criteria = new FilterExpression(LogicalOperator.And)
        {
            Conditions = {
            new ConditionExpression(
                attributeName:"primaryobjecttypecode",
                conditionOperator: ConditionOperator.Equal,
                value: entityLogicalName)
            }
        },
        LinkEntities = {
            new LinkEntity(
                linkFromEntityName:"sdkmessagefilter",
                linkToEntityName:"sdkmessage",
                linkFromAttributeName:"sdkmessageid",
                linkToAttributeName:"sdkmessageid",
                joinOperator: JoinOperator.Inner)
            {
                    LinkCriteria = new FilterExpression(LogicalOperator.And){
                    Conditions = {
                        new ConditionExpression(
                            attributeName:"name",
                            conditionOperator: ConditionOperator.Equal,
                            value: messageName)
                        }
                    }
            }
        }
    };

    EntityCollection entityCollection = service.RetrieveMultiple(query);

    return entityCollection.Entities.Count.Equals(1);
}

In the above code, check for the parameters highlighted in bold. You need to replace these parameters with your values at runtime. For example – If I want to check if CreateMultiple is supported for Account table, I need to set entityLogicalName to account and messageName to CreateMultiple.

As of the time of writing this blog, these messages are currently not supported for standard tables. So you cannot use these messages for out-of-box tables like Account, Contact etc. However it should be available for any custom tables you created recently and also for elastic tables.
Also as per MS Learn, these messages will be available for all standard tables in the future.

Once you have checked that the message is supported for a table, it’s time to put this message in action. Below is the sample code for CreateMultiple

 static void CreateMultiple(IOrganizationService client)
      {

         CreateMultipleRequest createMultipleRequest = new CreateMultipleRequest();

         EntityCollection entities = new EntityCollection();
         entities.EntityName = "cref2_milestones"; // This is important. If you don't set this explicitly, it will result in error.
         
         // set-up records for create
         var entMilestone1 = new Entity("cref2_milestones");
         entMilestone1["cref2_milestonetitle"] = "Student checklist 1 compeleted";

         var entMilestone2 = new Entity("cref2_milestones");
         entMilestone2["cref2_milestonetitle"] = "Student checklist 2 compeleted";

         // add the records to the collection
         entities.Entities.Add(entMilestone1);
         entities.Entities.Add(entMilestone2);

         // set the Targets property of the request.
         createMultipleRequest.Targets = entities;

         CreateMultipleResponse createMultipleResponse = (CreateMultipleResponse)client.Execute(createMultipleRequest);

        // parse the response
        foreach(var id in createMultipleResponse.Ids)
         {
            Console.WriteLine($"Guid: {id}");
         }
      }

The code is pretty simple and very similar to ExecuteMultiple and ExecuteTransaction request messages. However unlike them, you can only add or update records of the same table in CreateMultiple and UpdateMultiple.

Check for the code highlighted in bold. Setting the EntityName property of the Entity Collection is very important. If you don’t set this, you will receive the following error while executing the request – The ‘CreateMultiple’ method does not support entities of type ‘none’.

And that’s it. This should work just fine. In my next article, I will show how to write a plugin for CreateMultiple/ UpdateMultiple.

Hope this helped. For similar interesting topics on Microsoft.NET and Power platform, you can subscribe to my blog.

Debajit Dutta
Business Solutions MVP

1 thought on “How to use CreateMultiple and UpdateMultiple messages using DataVerse SDK Client”

Comments are closed.