Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Troubleshooting SDK.REST.RetrieveRecord

Posted on by

A couple years ago I posted here about how to use this method: https://community.dynamics.com/crm/f/117/t/242500

I solved the issue as noted there, but now I'm trying the same thing an another entity and it's not working.

The code is on the new_Invoice entity, and I'm trying to access a field on the out-of-box Account entity.

Here's my code:

var companyID = Xrm.Page.getAttribute('new_companyid').getValue()[0].id;
var APContact = null;
var name = null;

SDK.REST.retrieveRecord
(
  companyID,
  'Account',
  null, null,
  function (Account)
  {
    APContact = Account.new_AccountsPayableContactId;
  },

  function Error()
  {
    alert('There is an error in reading account data.');
  }
);


In all my tests, APContact has always ended up null within the function and after it. What am I doing wrong?

*This post is locked for comments

  • Verified answer
    DaveRB98 Profile Picture
    DaveRB98 on at
    RE: Troubleshooting SDK.REST.RetrieveRecord

    For those of you who are trying to solve a similar issue:

    I think my syntax was generally correct. The main issue was that my code didn't wait for the RetrieveRecords operation to complete before continuing. The people here helped me find confidence in my code, so I appreciate that. Here's my final, working code:

      // preparing to check if there's an AP Contact
      var companyID = Xrm.Page.getAttribute('new_companyid').getValue()[0].id;
      var APContact = null;
      var name = null;
    
      SDK.REST.retrieveRecord
      (
        companyID,
        'Account',
        "new_AccountsPayableContactId",
        null,
        function (Account)
        {
          APContact = Account.new_AccountsPayableContactId.Id;
          writeNotes(APContact);
        },
    
        function Error()
        {
          alert('There is an error in reading account data.');
        }
      );
    
      function writeNotes(APContact)
      {
        // perform logical operations with APContact here
      }


    Thanks everyone for your help!

  • DaveRB98 Profile Picture
    DaveRB98 on at
    RE: Troubleshooting SDK.REST.RetrieveRecord

    Yes, I saw that and fixed it. Thanks. :)

    Okay, so now the issue I'm seeing is that the code using the APContact variable executes before the RetrieveRecord function finishes its query, so the value of APContact is null. I'm going to try building a function call that executes within the retrieve function. When it's working, I'll go back through the thread and confirm solutions. :)

  • Suggested answer
    Sreevalli Profile Picture
    Sreevalli 3,256 on at
    RE: Troubleshooting SDK.REST.RetrieveRecord

    Hay,

    you are using "account" in place of "Account", use the function parameter & its case sensitive :)

  • DaveRB98 Profile Picture
    DaveRB98 on at
    RE: Troubleshooting SDK.REST.RetrieveRecord

    Well, I just realized the problem above is that Account <> account; fixing that gives me your results, Sreevalli. I'll get back to you.

  • DaveRB98 Profile Picture
    DaveRB98 on at
    RE: Troubleshooting SDK.REST.RetrieveRecord

    Thanks for your help so far, everyone! I think I'm close to having this solved.

    Sreevalli, I matched your code as closely as I could for my system. Here are the results:

    2019_2D00_01_2D00_08_5F00_08_2D00_38_2D00_10.jpg

    As you can see, the account is undefined. That would lead me to think I need to update my SDK . . . except that Goutam's code seems to work as expected. I'll format his code to my needs and get back to you with the results. The codes are similar, but not identical, so I'm curious to see why one seems to work while the other does not.

  • Verified answer
    Sreevalli Profile Picture
    Sreevalli 3,256 on at
    RE: Troubleshooting SDK.REST.RetrieveRecord

    Hi,

    I just coped your field name and code, its working as expected for me :D

    again check your field name and try to update SDK.Rest from latest SDK.

    codesnip.png

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Troubleshooting SDK.REST.RetrieveRecord

    Hi Dave,

    If the code suggested by Goutam is returning you the contact object then what else you are looking for? Can you please share your exact requirement?

  • DaveRB98 Profile Picture
    DaveRB98 on at
    RE: Troubleshooting SDK.REST.RetrieveRecord

    Sreevalli, yes, it is a lookup field that I am trying to retrieve.

    Thanks a bunch for the code, Goutam!! It works for debugging but I'm still trying to get a variable to be assigned the value so I can perform logical operations.

    Without ".Name", your code returned an object. With the ".Name" suffix, your code returned my name (which I was using as a test case). I'll continue to test and troubleshoot since my requirement is not fulfilled completely yet.

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Troubleshooting SDK.REST.RetrieveRecord

    Hi ,

    Try with this -

    // call this method

    function retrieveAccount() {

    var companyID = Xrm.Page.getAttribute('new_companyid').getValue()[0].id;

    SDK.REST.retrieveRecord(companyID, "Account", null, null, getDetails, errorHandler);

    }

    // I have use separate method here so that you can debug your result.

    function getDetails(account) {

    debugger;

    alert("Account Payble contact I'd  : " + account.new_AccountsPayableContactId.Name ); // Put .Name if it's lookup

    }

    function errorHandler(error) {

    alert(error.message);

    }

    You can refer below reference for more info -

    arunpotti.wordpress.com/.../restretrieveexample

  • Sreevalli Profile Picture
    Sreevalli 3,256 on at
    RE: Troubleshooting SDK.REST.RetrieveRecord

    Hi,

    hope its a lookup field you are trying to pull, let me replicate and update you.

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans