locked
SDK is undefined error RRS feed

  • Question

  • We have a JavaScript function called getUserBusinessUnit (see below) that we're using to retrieve the current user's business unit ID and business unit name.  This function is using the SDK.REST.retrieveRecord method and works perfectly if we run it from an event like the OnChange() of a field on say the Contact form.  Therefore, we know the function is working.

    We have now added a button to the Contact Homepage ribbon (ie above the main Contact Grid view) and we've configured it to only run the getUserBusinessUnit function when it is pressed.  However, it is raising the following error:  'SDK' is undefined.

    Is it possible that the SDK.REST.js and/or JSON2.js files are not being loaded before this function is called and that's why we're seeing this error? 

    function getUserBusinessUnit() {
    
        var SystemuserId = Xrm.Page.context.getUserId();
        
        SDK.REST.retrieveRecord(
         SystemuserId,
         "SystemUser",
         "BusinessUnitId", null,
         function (user) {
             alert(user.BusinessUnitId.Id);
             alert(user.BusinessUnitId.Name);
         },
         errorHandler
       );
    }


    • Edited by Cipher Wednesday, July 31, 2013 2:48 PM update
    Wednesday, July 31, 2013 2:35 PM

Answers

All replies

  • Hi,

    Are you including the SDK.REST.js in the Ribbon Custom Action (using the isNaN trick)?

    http://rajeevpentyala.wordpress.com/2012/05/23/loading-dependent-jscript-libraries-in-ribbon-buttons-execution-crm-2011/

    hth


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    • Marked as answer by Cipher Wednesday, July 31, 2013 4:59 PM
    Wednesday, July 31, 2013 3:34 PM
    Answerer
  • Thanks, Scott!  

    I was using the Visual ribbon Editor and after I had added the JavaScript method I thought that's all that was needed.  I didn't realize the homepage wasn't using the web resources I loaded in the Form Libraries window.

    Therefore, for others that might come across this:

    1) Calling a JS function from a form button works as long as all the JS files are loaded in the Form Libraries

    2) Calling a JS function from a home page button works as long as you specify the resource you are calling and any dependent resources they are calling (see Scott's link for a great example)

    Wednesday, July 31, 2013 5:06 PM