involve Batch Operation CRM 2016 Web Api
CRM 2016 has been support for execute Batch Operation which using API. Here are the basic steps that we can consume this amazing feature of CRM 2016.
Example situation: my company need 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;
}
Now we just add this CreateBatchTaskforAccount to OnSave of Account form (Call in create new account). After saving account, 2 tasks will be added for it.
This was originally posted here.

Like
Report
*This post is locked for comments