Skip to main content

Notifications

Announcements

No record found.

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

  • JohnAnonymous Profile Picture
    JohnAnonymous 5,241 on at
    RE: bulk update using batch process using JavaScript crm

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

  • ganeshm Profile Picture
    ganeshm on at
    RE: bulk update using batch process using JavaScript crm

    sure,thanks for your help though.

  • JohnAnonymous Profile Picture
    JohnAnonymous 5,241 on at
    RE: bulk update using batch process using JavaScript crm

    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
    ganeshm on at
    RE: bulk update using batch process using JavaScript crm

    yes the rest calls are working fine.

  • JohnAnonymous Profile Picture
    JohnAnonymous 5,241 on at
    RE: bulk update using batch process using JavaScript crm

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

  • Suggested answer
    ganeshm Profile Picture
    ganeshm on at
    RE: bulk update using batch process using JavaScript crm

    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.

  • Suggested answer
    JohnAnonymous Profile Picture
    JohnAnonymous 5,241 on at
    RE: bulk update using batch process using JavaScript crm

    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
    ganeshm on at
    RE: bulk update using batch process using JavaScript crm

    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

  • Verified answer
    JohnAnonymous Profile Picture
    JohnAnonymous 5,241 on at
    RE: bulk update using batch process using JavaScript crm

    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.

  • ganeshm Profile Picture
    ganeshm on at
    RE: bulk update using batch process using JavaScript crm

    just want to change the update function with batch process

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…

Vahid Ghafarpour – Community Spotlight

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

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,307 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans