Skip to main content

Notifications

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

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,969 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,842 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans