Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

SDK.REST.retrieveMultipleRecords is not working

Posted on by 1,530

Hi,

I have been working on this for a several hours.  I can't seem to see why this is not working.

function setSampleCodes() {

    var iselect = "$select=new_sampleid,new_name";
    var iorderby = "&$orderby=new_name";
    var options = iselect + iorderby;

    SDK.REST.retrieveMultipleRecords("new_sample", options, sampleCodeCallBack, errorHandler, retrieveComplete);
}

function sampleCodeCallBack(retrievedData) {
    var opt = new Option();
    if (retrievedData.length > 0) {
        for (var i = 0; i < retrievedData.length; i++) {
            opt[i].text = retrievedData[i].new_name;
            opt[i].value = retrievedData[i].new_Sampleid;
        }
        Xrm.Page.getAttribute("new_samplecode").setValue(opt);
    }
}

function retrieveComplete() {
    alert(Xrm.Page.gerAttribute("new_samplecodes").getValue());      
}

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


*This post is locked for comments

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: SDK.REST.retrieveMultipleRecords is not working

    Hi ,

    Yes its best practices to upgrade all our SDK dependency method because Microsoft introduce WEB API to reduce overhead of using SDK dependency . In future release MS will be deprecated the older way to retrieve data so its better to use the new WEB API instead of SDK.REST or XRMServiceToolKit.

  • rthompson Profile Picture
    rthompson 1,530 on at
    RE: SDK.REST.retrieveMultipleRecords is not working

    Hi Goutam,

    Why should I use WEB API if  SDK.REST also works.  Is best practice to use WEB.API

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: SDK.REST.retrieveMultipleRecords is not working

    Hello,

    Try to replace the code with WEB API like below - 

        var req = new XMLHttpRequest();
        req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_samples?$select=new_sampleid,new_name&$orderby=new_name asc", 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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 200) {
                    var results = JSON.parse(this.response);
                    for (var i = 0; i < results.value.length; i++) {
                        var id = results.value[i]["new_sampleid"];
                        var name = results.value[i]["new_name"];
                    }
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send();


    Change API version , if its different . 

    I have some doubt in your code  - What is the field  Type of new_samplecode, ? this is not the right way you are setting the value .

    function sampleCodeCallBack(retrievedData) {
        var opt = new Option();
        if (retrievedData.length > 0) {
            for (var i = 0; i < retrievedData.length; i++) {
                opt[i].text = retrievedData[i].new_name;
                opt[i].value = retrievedData[i].new_Sampleid;
            }
            Xrm.Page.getAttribute("new_samplecode").setValue(opt); 
        }
    }
  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: SDK.REST.retrieveMultipleRecords is not working

    Okay:

    1. try WebAPI instead - i doubt if Odata works in 2016 anymore.

    2. whatever you are trying to retrieve try to browse it in the browser by framing the odata URL.

  • Verified answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: SDK.REST.retrieveMultipleRecords is not working

    Hi,

    When you say you did debugging, did you put a breakpoint in the callback function.

    What is the retrieveData.length?

    Also, not sure if this is the case, but in case it does retrieve data, what is the data type of new_sampleid? Is it an integer or a Guid.

    One more thing I notice. In your iselect, the new_sampleid is all lowercase. In your for loop (callback function) the S is uppercase. This must match.

    Hope this helps.

  • rthompson Profile Picture
    rthompson 1,530 on at
    RE: SDK.REST.retrieveMultipleRecords is not working

    I did the bugging.  It's seem to only return a null.  I can't seem to find the exact error message.

  • Suggested answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: SDK.REST.retrieveMultipleRecords is not working

    Hi There,

    Its simple, Debug it - you will know where & on which exact line it is failing , also with what error.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans