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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

bulk update using batch process using JavaScript crm

(0) ShareShare
ReportReport
Posted on by

i have a contact entity dob,age fields. During onsave my account all the age in contact entity update based on my dob field.But only using batch process to bulk update my record in contact.
i have no other example on how to use batch process in javascript. 

debugger;
var now = new Date();
var today = new Date().getFullYear();
var odata = "$select=ContactId,new_age,new_dob&$filter=new_dob ne null";
var age = new Date().getFullYear();
SDK.REST.retrieveMultipleRecords("Contact", odata, function(results) {
debugger;
for (var i in results) {

var res = results[i].new_dob;
var newage = results[i].new_age;
var contactid = results[i].ContactId;
var birthyear = res.getFullYear();
if (birthyear != null) {
age=today-birthyear;
update(age, contactid)
}

}
}, errorHandler, function() {});


function errorHandler(error) {
writeMessage(error.message);
}

function update(age, contactid) {
var contact = {};
contact.new_age = age;
SDK.REST.updateRecord(contactid, contact, "Contact", function() {}, errorHandler);
}

*This post is locked for comments

I have the same question (0)
  • ganeshm Profile Picture
    on at

    just want to change the update function with batch process

  • Verified answer
    JohnAnonymous Profile Picture
    5,241 on at

    This is the documentation on creating batch requests: msdn.microsoft.com/.../mt607719.aspx

    I also wrote a blog on it here: www.oak3.org/.../webapi-batch-request and if you search there are some topics on this forum as well.

    Please let me know if you any specific questions.

  • Suggested answer
    ganeshm Profile Picture
    on at

    hey Thank for the reply ,helped me so much

    now i have created a batch process ,but i keeping getting error as 401(unauthorized).I tried authenticating the credential but still not solved,

    function mul() {
    debugger;

    //for
    var odata = "$select=ContactId,new_age,new_dob&$filter=new_dob ne null";
    SDK.REST.retrieveMultipleRecords("Contact", odata, function(results) {

    debugger;
    var batchId = "1006";
    var changeSetId = "2006";
    var contactList;
    var today = new Date().getFullYear();

    var age = new Date().getFullYear();
    payload = ["--batch_" + batchId]
    payload.push("Content-Type: multipart/mixed;boundary=changeset_" + changeSetId);
    payload.push("");
    for (var i in results) {
    var res = results[i].new_dob;
    var newage = results[i].new_age;
    var contactid = results[i].ContactId;
    var birthyear = res.getFullYear();
    if (birthyear != null) {
    age = today - birthyear;


    //First item in changeSetId
    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("PATCH " + "365new2016.api.crm8.dynamics.com/.../contacts(" + contactid + ") HTTP/1.1"); //re[i].id
    payload.push("Content-Type: application/json;type=entry");
    payload.push("");
    payload.push(JSON.stringify(age));
    //for end

    }
    }
    payload.push("--changeset_" + changeSetId + "--");
    payload.push("--batch_" + batchId + "--");
    var r = payload.join("\r\n");
    debugger;
    var req = new XMLHttpRequest();
    req.open("POST", "365new2016.api.crm8.dynamics.com/.../v8.2" + "$batch");

    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.send(r);

    }, errorHandler, function() {});

    function errorHandler(error) {
    writeMessage(error.message);
    }
    }
    40513.1.png

  • Suggested answer
    JohnAnonymous Profile Picture
    5,241 on at

    If you're calling the api from within CRM you shouldn't need to authenticate. Are you calling the api from the CRM context?

  • Suggested answer
    ganeshm Profile Picture
    on at

    yes.i am calling the api inside the crm context.

    but i tried without authentication(in the pic i have commented the authenticate)still getting the same error.

  • JohnAnonymous Profile Picture
    5,241 on at

    Other REST calls are working? Have you tried with a tool like Postman or Advance REST Client?

  • ganeshm Profile Picture
    on at

    yes the rest calls are working fine.

  • JohnAnonymous Profile Picture
    5,241 on at

    I'm sorry, but I don't see any issues with your call. Try opening a new thread on this specific subject. Maybe somebody else has an idea.

  • ganeshm Profile Picture
    on at

    sure,thanks for your help though.

  • JohnAnonymous Profile Picture
    5,241 on at

    Could you help out the community by marking answers as verified when people helped you out?

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans