I want to update/delete 20 quote lines. I googled it and but i didn't find any idea how to use CRM web API batch request.
Could you please any one share CRM web API batch request using javascript?
*This post is locked for comments
Should be simple, one block for a POST message to create a record and one for a PATCH message to update the record. My blog shows an example of POST (create) and DELETE.
Thanks for sharing.
How batch request differentiate payload for create and update ?
Hi Alagu,
I've posted a sample on my blog: www.oak3.org/.../webapi-batch-request
Hi Alagunellaikumar,
Please try the following code. This is an example to create multiple task for new account.
//Create 1 Js web resource method that will handle batch request function executeBatch (payload, batchId, successCallback, errorCallback, callerId) { var req = new XMLHttpRequest(); req.open(“POST”, “Your Web Api Here” + “$batch”), true); req.setRequestHeader(“Accept”, “application/json”); req.setRequestHeader(“Content-Type”, “multipart/mixed;boundary=batch_” + batchId); req.setRequestHeader(“OData-MaxVersion”, “4.0”); req.setRequestHeader(“OData-Version”, “4.0”); req.onreadystatechange = function () { if (this.readyState == 4 /* complete */) { req.onreadystatechange = null; if (this.status == 200) { if (successCallback) { successCallback(this.response); } } else { if (errorCallback) errorCallback(Sdk.WebApiOperation.errorHandler(this)); } } }; req.send(payload); } //Create CreateBatchTaskforAccount method to create task for new account function CreateBatchTaskforAccount(accountUri ) { //Generating random id for the payload object var batchId = GetRandomId(); //Define tasks to be created: var firstTask = { subject: “new task for new account” + accountUri, “regardingobjectid_account_task@odata.bind”: accountUri }; //Note: Imortant here var secondTask = { subject: “second task”, “regardingobjectid_account_task@odata.bind”: accountUri }; //Start of ChangeSet payload = [“–batch_” + batchId] payload.push(“Content-Type: multipart/mixed;boundary=changeset_” + changeSetId); payload.push(“”); //First item in ChangeSet payload.push(“–changeset_” + changeSetId); payload.push(“Content-Type: application/http”); payload.push(“Content-Transfer-Encoding:binary”); payload.push(“Content-ID: 1”); payload.push(“”); payload.push(“POST ” + CrmSdk.RBEIApi.getWebApiUrl() + “tasks HTTP/1.1”); payload.push(“Content-Type: application/json;type=entry”); payload.push(“”); payload.push(JSON.stringify(firstTask)); //Second item in ChangeSet payload.push(“–changeset_” + changeSetId); payload.push(“Content-Type: application/http”); payload.push(“Content-Transfer-Encoding:binary”); payload.push(“Content-ID: 2”); payload.push(“”); payload.push(“POST ” + CrmSdk.RBEIApi.getWebApiUrl() + “tasks TTP/1.1”); payload.push(“Content-Type: application/json;type=entry”); payload.push(“”); payload.push(JSON.stringify(secondTask)); //End of ChangeSet payload.push(“–changeset_” + changeSetId + “–“); payload.push(“”); //Adding a GET request outside of the ChangeSet payload.push(“–batch_” + batchId); payload.push(“Content-Type: application/http”); payload.push(“Content-Transfer-Encoding:binary”); payload.push(“”); //Retrieve all the tasks related to the account payload.push(“GET ” + accountUri + “/Account_Tasks?$select=subject HTTP/1.1”); payload.push(“Accept: application/json”); payload.push(“”); payload.push(“–batch_” + batchId + “–“); executeBatch(payload.join(“\r\n”), batchId, function (response) { alert(response); }, function (error) { alert(error); }); } //Generating random id for Payload object Function GetRandomId (idLength) { if (this.isNullOrUndefined(idLength)) idLength = 10; if (this.isNumber(idLength)) { if (idLength > 30) { throw new Error(“Length must be less than 30.”); } } else { throw new Error(“Length must be a number.”); } var returnValue = “”; var characters = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789”; for (var i = 0; i < idLength; i++) returnValue += characters.charAt(Math.floor(Math.random() * characters.length)); return returnValue; }
Hi Martijn,
If you don't mind, could you please give me full code for create batch request for task? I don't understand, how should i use properly?
What you can do in javascript is the following
//create an empty array var payload = []; //push objects into that array payload.push('--batch_340293023'); payload.push('Content-Type......'); payload.push(''); //create the payload that you can use in the post message var postdata = payload.join('\r\n');
This worked for me.
Hi,
Please go through the following link.
community.dynamics.com/.../involve-batch-operation-crm-2016-web-api
How should i create a payload for this? I could not understand the payload for batch request
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,232 Super User 2024 Season 2
Martin Dráb 230,064 Most Valuable Professional
nmaenpaa 101,156