web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to use CRM web API batch request using javascript

(0) ShareShare
ReportReport
Posted on by 6,212

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

I have the same question (0)
  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at
    RE: How to use CRM web API batch request using javascript

    Hi Alagu nellaikumar,

    Please refer the link below:

    stackoverflow.com/.../crm-webapi-batch-request

  • Alagunellaikumar Profile Picture
    6,212 on at
    RE: How to use CRM web API batch request using javascript

    How should i create a payload for this? I could not understand the payload for batch request

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at
    RE: How to use CRM web API batch request using javascript

    Hi,

    Please go through the following link.

    community.dynamics.com/.../involve-batch-operation-crm-2016-web-api

  • Suggested answer
    JohnAnonymous Profile Picture
    5,241 on at
    RE: How to use CRM web API batch request using javascript

    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.

  • Alagunellaikumar Profile Picture
    6,212 on at
    RE: How to use CRM web API batch request using javascript

    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?

  • Verified answer
    Nithya Gopinath Profile Picture
    17,078 on at
    RE: How to use CRM web API batch request using javascript

    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;
    }
  • Verified answer
    JohnAnonymous Profile Picture
    5,241 on at
    RE: How to use CRM web API batch request using javascript

    Hi Alagu,

    I've posted a sample on my blog: www.oak3.org/.../webapi-batch-request

  • Alagunellaikumar Profile Picture
    6,212 on at
    RE: How to use CRM web API batch request using javascript

    Thanks for sharing.

    How batch request differentiate payload for create and update  ?

  • Verified answer
    JohnAnonymous Profile Picture
    5,241 on at
    RE: How to use CRM web API batch request using javascript

    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.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans