Hi,
I wan to create multiple task on account entity using batch request.
I am referring this two blogs to create a batch request:
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)

Report
All responses (
Answers (