locked
retrieve crm data using xmlhttprequest from the crm form RRS feed

  • Question

  • Hi,

    I'm using CRM 2015. I'm trying to retrieve contract data by using xmlhttprequest.

    I used this:

    xHReq.open("GET", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/ContractSet?$select=ACCESS_ALS_habilites,ContractId", true);
    xHReq.setRequestHeader("Accept", "application/json");
    xHReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    xHReq.onreadystatechange = function () {
        if (this.readyState === 4) {
            this.onreadystatechange = null;
            if (this.status === 200) {
                var returned = JSON.parse(this.responseText).d;
                var results = returned.results;
                for (var i = 0; i < results.length; i++) {
                     var aCCESS_ALS_habilites = results[i].ACCESS_ALS_habilites;
                     var contractId = results[i].ContractId;

                }//end for
                     }
            else {
                alert(this.statusText);
            }
        }
    };
    xHReq.send();

    When i check the length of result i had 50. but when i check with a sql query i had 3000. How can i solve this issue?

    thanks for help.

    Regards

    Thursday, February 25, 2016 1:17 PM

Answers

  • Hi,

    Odata will always return 50 records at a time. Try this.

    You can page through records and get next set of records. 

    if you want everything in one request You can Use Soap request. It will return 5000 records per request.

    Thanks!

    Kalim


    Thursday, February 25, 2016 2:40 PM
  • Hi,

    i've seen that always return 50 records.

    I used crm rest builder to retrieve xmlhttp request and i page through records and get next set of records with json. I don't use ajax like you made Karim, but i used your method.

    Thanks,

    Now it's ok

    here is a sample:

    function GetRecords(url ){

    var xHReq=getXMLHttpRequest();

    xHReq.open("GET",url, true);
    xHReq.setRequestHeader("Accept", "application/json");
    xHReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    xHReq.onreadystatechange = function () {
        if (this.readyState === 4) {
            this.onreadystatechange = null;
            if (this.status === 200) {
             var returned = JSON.parse(this.responseText).d;
      var results = returned.results;

    if( JSON.parse(this.responseText)&&returned!=null&& results!=null){

             AddRecordsToArray(results);
            FetchRecordsCallBack(returned);
    }else{
    alert('result null ou non défini');

    }
    //****************************************************************************
    //alert(' tableau rempli ' + results.length);

      for (var i = 0; i < results.length; i++) {
                 var aCCESS_ALS_habilites = results[i].ACCESS_ALS_habilites;
                    var contractId = results[i].ContractId;

    if( aCCESS_ALS_habilites !=null){
    var nomcontacts= aCCESS_ALS_habilites.split("\n");
    for(var y=0; y< nomcontacts.length; y++){

    var nom=nomcontacts[y];

    }//end for
    Xrm.Page.getAttribute("access_interlocuteursagrs").setValue("\r"+nom);

    }//end if
    else{
    alert('interlocuteurs null  ');

    }//end else

               }//end for


                     }//end if
            else {
                alert(this.statusText);
            }//end else


        }//end (this.readyState )
    //******************************************************
    };
    //end function xHReq.onreadystatechange

    xHReq.send();

    }//end function  GetRecords

    • Marked as answer by dc74 Wednesday, March 2, 2016 3:43 PM
    Wednesday, March 2, 2016 3:42 PM

All replies

  • Hi,

    That query should give you all your contracts in CRM, what happens if you use the search in a web browser? BTW, you don't need to fetch ContractId, you'll get that anyway.

    Regards


    Rickard Norström Developer CRM-Konsulterna
    http://www.crmkonsulterna.se
    Swedish Dynamics CRM Forum: http://www.crmforum.se
    My Blog: http://rickardnorstrom.blogspot.se

    Thursday, February 25, 2016 2:33 PM
  • Hi,

    Odata will always return 50 records at a time. Try this.

    You can page through records and get next set of records. 

    if you want everything in one request You can Use Soap request. It will return 5000 records per request.

    Thanks!

    Kalim


    Thursday, February 25, 2016 2:40 PM
  • Hi,

    i've seen that always return 50 records.

    I used crm rest builder to retrieve xmlhttp request and i page through records and get next set of records with json. I don't use ajax like you made Karim, but i used your method.

    Thanks,

    Now it's ok

    here is a sample:

    function GetRecords(url ){

    var xHReq=getXMLHttpRequest();

    xHReq.open("GET",url, true);
    xHReq.setRequestHeader("Accept", "application/json");
    xHReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    xHReq.onreadystatechange = function () {
        if (this.readyState === 4) {
            this.onreadystatechange = null;
            if (this.status === 200) {
             var returned = JSON.parse(this.responseText).d;
      var results = returned.results;

    if( JSON.parse(this.responseText)&&returned!=null&& results!=null){

             AddRecordsToArray(results);
            FetchRecordsCallBack(returned);
    }else{
    alert('result null ou non défini');

    }
    //****************************************************************************
    //alert(' tableau rempli ' + results.length);

      for (var i = 0; i < results.length; i++) {
                 var aCCESS_ALS_habilites = results[i].ACCESS_ALS_habilites;
                    var contractId = results[i].ContractId;

    if( aCCESS_ALS_habilites !=null){
    var nomcontacts= aCCESS_ALS_habilites.split("\n");
    for(var y=0; y< nomcontacts.length; y++){

    var nom=nomcontacts[y];

    }//end for
    Xrm.Page.getAttribute("access_interlocuteursagrs").setValue("\r"+nom);

    }//end if
    else{
    alert('interlocuteurs null  ');

    }//end else

               }//end for


                     }//end if
            else {
                alert(this.statusText);
            }//end else


        }//end (this.readyState )
    //******************************************************
    };
    //end function xHReq.onreadystatechange

    xHReq.send();

    }//end function  GetRecords

    • Marked as answer by dc74 Wednesday, March 2, 2016 3:43 PM
    Wednesday, March 2, 2016 3:42 PM