0

I am trying to insert multiple records using JavaScript web resource in PowerApps model driven application. Currently I am able to insert one record into the table using below HTTP request.

var req = new XMLHttpRequest();
req.open("POST", "[Organization URI]/api/data/v9.2/[entity name]", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", 'odata.include-annotations="*"');
req.onreadystatechange = function () {
  if (this.readyState === 4) {
    req.onreadystatechange = null;
    if (req.status === 200 || req.status === 204) {
      console.log(JSON.parse(req.responseText));
    } else {
      console.log("Error code: " + req.status);
    }
  }
};
const body = JSON.stringify({
  prop1: "12345",
  prop2: "test data",
});
req.send(body);

Now I am trying to insert multiple records in single HTTP request, but unable to do so. Below is the code which I have tried implementing

var req = new XMLHttpRequest();
req.open("POST", "[Organization URI]/api/data/v9.2/[entity name]", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", 'odata.include-annotations="*"');
req.onreadystatechange = function () {
  if (this.readyState === 4) {
    req.onreadystatechange = null;
    if (req.status === 200 || req.status === 204) {
      console.log(JSON.parse(req.responseText));
    } else {
      console.log("Error code: " + req.status);
    }
  }
};
const body = JSON.stringify([
  {
    prop1: "12345",
    prop2: "test data",
  },
  {
    prop1: "12345",
    prop2: "test data",
  },
]);
req.send(body);

But getting below error

{"error":{"code":"0x80048d19","message":"Error identified in Payload provided by the user for Entity :'[Entity Name]', For more information on this error please follow this help link https://go.microsoft.com/fwlink/?linkid=2195293 ----> InnerException : Microsoft.OData.ODataException: Invalid JSON. More than one value was found at the root of the JSON content. JSON content can only have one value at the root level, which is an array, an object or a primitive value.

I tried below body format too, but still not able to identify the solution

const body = JSON.stringify(
  {
    prop1: "12345",
    prop2: "test data",
  },
  {
    prop1: "12345",
    prop2: "test data",
  }
);

Kindly let me know if there is any way in JavaScript to insert multiple records in a single request.

Thanks in advance

EDIT 1:

As per suggestion from Guido Prieite I tried executing the batch request. Below is the code which I have tried.

var req = new XMLHttpRequest();
req.open("POST", "[Organization URI]/api/data/v9.2/$batch", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", 'multipart/mixed; boundary="batch_80dd1615-2a10-428a-bb6f-0e559792721f"');
req.setRequestHeader("Prefer", 'odata.include-annotations="*"');
req.onreadystatechange = function () {
  if (this.readyState === 4) {
    req.onreadystatechange = null;
    if (req.status === 200 || req.status === 204) {
      console.log(JSON.parse(req.responseText));
    } else {
      console.log("Error code: " + req.status);
    }
  }
};
const body = '--batch_80dd1615-2a10-428a-bb6f-0e559792721f\nContent-Type: application/http\nContent-Transfer-Encoding: binary\n\nPOST /api/data/v9.2/[Entity Name] HTTP/1.1\nContent-Type: application/json; type=entry\n\n{\n"prop1": "1234567",\n"prop2": "test1"\n}\n--batch_80dd1615-2a10-428a-bb6f-0e559792721f\nContent-Type: application/http\nContent-Transfer-Encoding: binary\nPOST /api/data/v9.2/[Entity Name] HTTP/1.1\nContent-Type: application/json; type=entry\n\n{\n"prop1": "123456789",\n"prop2": "test new new"\n}\n--batch_80dd1615-2a10-428a-bb6f-0e559792721f';
req.send(body);

But I am receiving below error

ExceptionMessage":"The message header 'POST /api/data/v9.2/[Entity Name] HTTP/1.1' is invalid. The header value must be of the format 'header name: header value'.","ExceptionType":"Microsoft.OData.ODataException"

Any help on this

RBK
  • 123
  • 1
  • 15
  • What is `[Entity Name]`? Are you actually supplying that exact string or substituting with some other value? – Professor Abronsius Jul 12 '23 at 06:30
  • Actually we have replaced the original table/entity name with [Entity Name] text. – RBK Jul 12 '23 at 07:17
  • It wont work as this request can only post one record at a time. U can try executemultiplerequest using javascript to achieve ur requirements. Preferrably use odata rest builder to frame ur requests – Harinarayanan Jul 12 '23 at 10:33
  • Do you have any reference code, so that I can try and implement at my end. Since I am new in the dataverse field – RBK Jul 12 '23 at 10:35
  • you need to batch the requests https://learn.microsoft.com/en-us/power-apps/developer/data-platform/webapi/execute-batch-operations-using-web-api – Guido Preite Jul 12 '23 at 15:31
  • @GuidoPreite I have tried out your suggestion, but I have received the error. Can you just check. The code which I have tried is been edited in the question – RBK Jul 13 '23 at 05:27
  • probably it has to do on how you are encoding the body to be multiline, but without having the exact endpoints is impossible to test (keep in mind that the full url should be inside each request of the batch) if you need an example on the batch using jquery (my example is a collection of GET requests) you can check this https://github.com/GuidoPreite/DRB/blob/0e7d46d9fd13da8d523c517241bc06960a4c4f63/js/drb.xrm.js#L147 – Guido Preite Jul 13 '23 at 14:00

0 Answers0