Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

How to use CRM web API batch request using javascript

Posted on by 6,210

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

  • Verified answer
    M.T. Eikelenboom Profile Picture
    M.T. Eikelenboom 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.

  • Alagunellaikumar Profile Picture
    Alagunellaikumar 6,210 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
    M.T. Eikelenboom Profile Picture
    M.T. Eikelenboom 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

  • Verified answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 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;
    }
  • Alagunellaikumar Profile Picture
    Alagunellaikumar 6,210 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?

  • Suggested answer
    M.T. Eikelenboom Profile Picture
    M.T. Eikelenboom 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.

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 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

  • Alagunellaikumar Profile Picture
    Alagunellaikumar 6,210 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
    Nithya Gopinath 17,074 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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,232 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,064 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans