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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Using batch request to create task on account entity

(0) ShareShare
ReportReport
Posted on by

Hi, 

I wan to create multiple task on account entity using batch request.

I am referring this two blogs to create a batch request:

1. https://community.dynamics.com/crm/b/xrmcenter/archive/2016/03/29/involve-batch-operation-crm-2016-web-api

2. http://www.oak3.org/crm/webapi-batch-request/

code which I have written is:

// JavaScript source code
function execute(batchId, payload, successCallback, errorCallback)
{
$.ajax(
{
    method: 'POST',
    contentType: "application/json; charset=utf-8",
    url: encodeURI(getWebAPIPath() + "$batch"),
    datatype: "json",
    headers: {
        'Content-Type': 'multipart/mixed;boundary=batch_' + batchId,
        'Accept': 'application/json',
        'OData-MaxVersion': '4.0',
        'OData-Version':  '4.0'
    },
    data: payload
});
}

function data() {
    var batchId = randomNumber();
    var changeSetId = randomNumber();
    var Id = "BB9C9CCE-7932-E811-A959-000D3A34A108";
    var task1 = {
        "subject": "Task 1 in batch",
        "regardingobjectid_account_task@odata.bind": getWebAPIPath() + "/api/data/v9.0/accounts" + "(" + Id + ")"
    };

    var task2 = {
        "subject": "Task 2 in batch",
        "regardingobjectid_account_task@odata.bind": getWebAPIPath() + "/api/data/v9.0/accounts" + "(" + Id + ")"
    };

    var data = [];

    // start of first change set
    data.push('--batch_' + batchId);
    data.push('Content-Type: multipart/mixed;boundary=changeset_' + changeSetId);
    data.push('');

    data.push("--changeset_" + changeSetId);
    data.push("Content-Type: application/http");
    data.push("Content-Transfer-Encoding:binary");
    data.push('Content-ID:1');
    data.push("");

    /// post method task1 ie create task on account 
    data.push('POST' + getWebAPIPath() + 'tasks HTTP/1.1');
    data.push("Content-Type: application/json;type=entry");
    data.push("");
    data.push(JSON.stringify(task1));

    /// second item in change set

    data.push("--changeset_" + changeSetId);
    data.push("Content-Type: application/http");
    data.push("Content-Transfer-Encoding:binary");
    data.push('Content-ID:1');
    data.push("");

    /// post method task ie create task on account 
    data.push('POST' + getWebAPIPath() + 'tasks HTTP/1.1');
    data.push("Content-Type: application/json;type=entry");
    data.push("");
    data.push(JSON.stringify(task2));

    // end of change set
    data.push("--changeset_" + changeSetId + "--");
    data.push("");

    //Adding a GET request outside of the ChangeSet
    data.push('--batch_' + batchId);
    data.push("Content-Type: application/http");
    data.push("Content-Transfer-Encoding:binary");
    data.push("");

    // get all task related to account
    data.push("GET " + getWebAPIPath() + "/Account_Tasks?$select=subject HTTP/1.1");
    data.push("Accept: application/json");
    data.push("");
    data.push('--batch_' + batchId + '--');

    var payload = data.join('\r\n');

    //call batch request

    execute(batchId, payload);
}

    function randomNumber() {
        var idLength = 10;
        var returnValue = "";
        var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        for (var i = 0; i < idLength; i++)
            returnValue += characters.charAt(Math.floor(Math.random() * characters.length));
        return returnValue;
    }

    function getWebAPIPath() {
        return Xrm.Page.context.getClientUrl() + "/api/data/v9.0/";
    }


*This post is locked for comments

I have the same question (0)

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans