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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

REST Web api in Javascript

(0) ShareShare
ReportReport
Posted on by 550

Hi,

Is it possible to "POST" or"PUT" method in javascript to update the result entity?

I need to update (address field) all contacts associated with an Account with the address field of account.

I tried  it but as of now eith "GET" request . Is there any other request to update the resultant entities using REST.

The below code is not fully working code (for info)

function AccountOnLoad() {
    debugger;
    var Id = Xrm.Page.data.entity.getId().substring(1, 37);
    var serverURL = Xrm.Page.context.getClientUrl();
    serverURL += "/api/data/v8.0/contacts?$select=address1_line1,address1_line2,address1_line3,address1_city,address1_stateorprovince,address1_postalcode,address1_country&$filter=_accountid_value eq " + Id + "&$count=true";
    var req = new XMLHttpRequest();
    req.open("GET", srverURL, true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    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) {
                var data = JSON.parse(this.response);
                if (data['@odata.count'] != null) {
                    if (Xrm.Page.getAttribute("address1_city") != null)
                    { result["address1_city"] = Xrm.Page.getAttribute("address1_city").getValue(); };
                    if (Xrm.Page.getAttribute("address1_country") != null)
                    { };
                    if (Xrm.Page.getAttribute("address1_line1") != null) { };
                    if (Xrm.Page.getAttribute("address1_line2") != null) { };
                    if (Xrm.Page.getAttribute("address1_line3") != null) { };
                    if (Xrm.Page.getAttribute("address1_stateorprovince") != null) { };
                    if (Xrm.Page.getAttribute("address1_postalcode") != null) { };
                    if (result["address1_city"] != null) {
                        var accountAddressCity = result["address1_city"];
                        Xrm.Page.getAttribute("address1_city").setValue(accountAddressCity);
                    }
                    if (result["address1_country"] != null) {
                        var accountAddressCountry = result["address1_country"];
                        Xrm.Page.getAttribute("address1_country").setValue(accountAddressCountry);
                    }
                    if (result["address1_line1"] != null) {
                        var accountAddressline1 = result["address1_line1"];
                        Xrm.Page.getAttribute("address1_line1").setValue(accountAddressline1);
                    }
                    if (result["address1_line2"] != null) {
                        var accountAddressline2 = result["address1_line2"];
                        Xrm.Page.getAttribute("address1_line2").setValue(accountAddressline2);
                    }
                    if (result["address1_line3"] != null) {
                        var accountAddressline3 = result["address1_line3"];
                        Xrm.Page.getAttribute("address1_line3").setValue(accountAddressline3);
                    }
                    if (result["address1_line3"] != null) {
                        var accountStateorProvience = result["address1_stateorprovince"];
                        Xrm.Page.getAttribute("address1_stateorprovince").setValue(accountStateorProvience);
                    }
                    if (result["address1_postalcode"] != null) {
                        var accountpostalCode = result["address1_postalcode"];
                        Xrm.Page.getAttribute("address1_postalcode").setValue(accountpostalCode);
                    }
                   
                } else {
                    var error = JSON.parse(this.response).error;
                    alert(error.message);
                }
            }
        }

    }; req.send();
}


*This post is locked for comments

I have the same question (0)
  • Verified answer
    Alagunellaikumar Profile Picture
    6,212 on at

    Hi

    Yes it is possible to update POST as well as PUT in CRM web API

  • HajiraRoshan Profile Picture
    550 on at

    Hi Nellaikumar,

    I am getting an undefined error on this line

    req.open("GET", srverURL, true);

    but when I try to call from browser the URL is working

    error:

    there is n error with the fields customized event

    Field: window,

    Error : undefined

    Event: onload

    if I use "PUT" or "POST" also getting the same error ..

    can anybody please help ..

    Thanks ,

    Hajira

  • Verified answer
    Mahendar Pal Profile Picture
    45,095 on at

    I will suggest you to use Web API Builder, it will be easy for you: github.com/.../CRMRESTBuilder

    Thanks

  • Verified answer
    ashlega Profile Picture
    34,477 on at

    You have an error in your code:

    req.open("GET", srverURL, true);

    should be

    req.open("GET", serverURL, true);
  • HajiraRoshan Profile Picture
    550 on at

    Hi ,

    Thanks for your reply!!

    I have found out that error..

    I am trying to do bulk update in contact(the records contact subgrids) records from account record . Copying and pasting account record address fields and pasting to all contact records address fields. I tried via rest protocol(PATCH) But does not seam to work. . But it seams that the update is only possible for a single record and bulk update is not possible with REST..

    Is there a way of updating records possible with rest protocol?

  • Verified answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hi HajiraRoshan,

    You can collect your result set and run your update under loop to update all of your result set. Or you can use WEB API for this, here is the sample which should help you: community.dynamics.com/.../web-api-bulk-operations

    Thanks

  • Verified answer
    HajiraRoshan Profile Picture
    550 on at

    Hi Himbap,

    Thanks for your reply ,

    I used web api to GET all contacts using retrieve multiple and then updated all contact records one by one using for loop and PATCH  method  within the first RETRIEVE function ..This method works ..

    function updateContactAddress() {

       try {

           var contactId;

           var Id = Xrm.Page.data.entity.getId().substring(1, 37);

           var serverURL = Xrm.Page.context.getClientUrl();

           serverURL += "/api/data/v8.2/contacts?$select=contactid&$filter=_accountid_value eq " + Id + "&$count=true";

           var req = new XMLHttpRequest();

           req.open("GET", serverURL, true);

           req.setRequestHeader("Accept", "application/json");

           req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

           req.setRequestHeader("OData-MaxVersion", "4.0");

           req.setRequestHeader("OData-Version", "4.0");

           req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");

           if(Id != "" ){

           req.onreadystatechange = function () {

               if (this.readyState == 4 /* complete */) {

                   if (this.status == 200) {

                       var data = JSON.parse(this.response);

                       var results = data.value;

                       for (var i = 0; i < results.length; i++) {

                           contactId = results[i].contactid;

                           updateAccount(contactId);

                       }

                   }

                   else {

                       var error = JSON.parse(this.response).error;

                       alert(error.message);

                   }

               }

           };

           req.send();

       }

    }

       catch (err) { alert(err.message); }

    }

    function updateAccount(contactId) {

      var address1_country = Xrm.Page.getAttribute("address1_country").getValue();

      var address1_city =Xrm.Page.getAttribute("address1_city").getValue();

       var address1_line1=Xrm.Page.getAttribute("address1_line1").getValue();

        var address1_line2=Xrm.Page.getAttribute("address1_line2").getValue();

        var address1_line3=Xrm.Page.getAttribute("address1_line3").getValue();

        var address1_stateorprovince=Xrm.Page.getAttribute("address1_stateorprovince").getValue();

        var address1_postalcode=Xrm.Page.getAttribute("address1_postalcode").getValue();

       var entity = {};

       entity.address1_city = address1_city ;

       entity.address1_country=address1_country ;

       entity.address1_line1=address1_line1;

       entity.address1_line2=address1_line2;

       entity.address1_line3=address1_line3;

       entity.address1_stateorprovince=address1_stateorprovince;

      entity.address1_postalcode=address1_postalcode;

    var req = new XMLHttpRequest();

    req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts(" + contactId + ")", true);

    req.setRequestHeader("OData-MaxVersion", "4.0");

    req.setRequestHeader("OData-Version", "4.0");

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.onreadystatechange = function() {

       if (this.readyState === 4) {

           req.onreadystatechange = null;

           if (this.status === 204) {

               //Success - No Return Data - Do Something

           } else {

               Xrm.Utility.alertDialog(this.statusText);

           }

       }

    };

    req.send(JSON.stringify(entity));

    }

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans