locked
CRM 2011: SDK.REST and JavaScript RRS feed

  • Question

  • Hey everyone. Here is the function I am having trouble with:

    function alertTestEntities()
    {
    
        SDK.REST.retrieveMultipleRecords(
            "new_testEntity",
            "$select=*",
            function (results) {
                alert("Success");
            },
            function (results) {
                alert(error.message);
            },
            function (results) {
                alert("Complete");
            }
        );
    
    }

    I added two web resources to the form of my "testEntity". The first contains only this function. The second contains the SDK.REST package.

    The function "alertTestEntities" is called on form load.

    When I load the form, I get an alert that says this:

    How can I properly use the SDK.REST method?



    • Edited by RyanNHG Wednesday, July 10, 2013 1:57 PM More relevant header
    Wednesday, June 26, 2013 7:18 PM

Answers

  • Hello everyone, I have come across the solution to my issue.

    The key was looking at the SDK.REST code. I didn't know what "Error : 0: :" meant, so I found the function that called it. in SDK.REST_errorHandler, the "0" represented the error status code.

    I searched to see what the status code "0", and apparently it means that I was not getting a response at all.

     

    The reason? The Same-Origin Policy

    My CRM form had the header: " http://mycrm.mycompany.com/MYCRM "...

    My server url was returning: " http://mycrm/MYCRM "...

    Though the protocols matched (both http), and both "mycrm" and "mycrm.mycompany.com" both pointed to the same domain, their domain strings were not identical, and this mismatch did not allow data to be retrieved.

    I solved this problem by modifying SDK.REST._getServerURL to return " http://mycrm.mycompany.com/MYCRM ", the url that matched where I called my script.

    • Marked as answer by RyanNHG Wednesday, July 10, 2013 1:54 PM
    Wednesday, July 10, 2013 1:53 PM

All replies

  • Check the first parameter... you need to use the schema name.

    So for your custom entity is it probably 'new_testEntity'


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Wednesday, June 26, 2013 8:01 PM
    Moderator
  • No luck, I'm getting the same results.


    How does my web resource know that the SDK.REST methods exist without referencing them anywhere in the code?
    • Edited by RyanNHG Wednesday, June 26, 2013 8:05 PM Addition
    Wednesday, June 26, 2013 8:03 PM
  • You would create a web resource with the SDK.REST code from the SDK, upload it to CRM and then include it in you form and make sure it appears before your script resource with your custom code. 

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Wednesday, June 26, 2013 8:08 PM
    Moderator
  • Also on the error callback - change the input parameter to 'error' instead of 'results' and the final complete callback - remove 'results' all together.

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Wednesday, June 26, 2013 8:11 PM
    Moderator
  • Arrange the web resources like this? "hbs_sdkRest" contains my SDK.REST code, and "hbs_zzHaskellScriptTest" contains the function.

    I made the "results" to "error" change and removed the complete  parameter altogether. Still running into the same alert.

    Wednesday, June 26, 2013 8:16 PM
  • I would say double check the schema name for the entity - it might be all lowercase as well - 'new_testentity'

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Wednesday, June 26, 2013 8:24 PM
    Moderator
  • I keep getting the same error message. I'm going to try to take a closer look at this code.

    I appreciate all of your assistance, Jason. If you come up with anything else, I'd love to give it a shot.
    Wednesday, June 26, 2013 8:32 PM
  • Hi,

    Are you sure your entity is named testEntity? It usually begins with your publisher code ("new_" if you are using the default publisher)

    Chris

    Wednesday, June 26, 2013 8:34 PM
  • Sorry, I changed that earlier, but didn't fix the code. Any other ideas, Chris?
    Wednesday, June 26, 2013 8:40 PM
  • You could also download and install this solution: CRM 2011 OData Query Designer

    It provides a GUI for creating the OData URL - you can pick from existing items and it will build the URL for you, in this case you could then grab the relevant pieces (entity name & select clause) from the output and put them into the SDK.REST.retrieveMultipleRecords call.


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Wednesday, June 26, 2013 8:46 PM
    Moderator
  • The Entity Name and Select Clause match up with the ones given in the OData Query Designer. What next?
    Thursday, June 27, 2013 5:04 PM
  • Just to see where we are at, can you post the current version of the script?


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Thursday, June 27, 2013 5:06 PM
    Moderator
  • function findTotalInternTests()
    {
    
        SDK.REST.retrieveMultipleRecords(
            "hbs_zzinterntestSet",
            "$select=*",
            function (results) {
                alert("Success");
            },
            function (error) {
                alert(error.message);
            },
            function () {
                alert("Complete");
            }
            );
    
    }

    I updated the entity I was looking for.
    Thursday, June 27, 2013 6:03 PM
  • Remove "Set" from your entity name (should have mentioned that) - the method appends this automatically when creating the URL.

    Otherwise that should work. I created an entity with the same name and publisher and received a success alert after running it. 


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Thursday, June 27, 2013 6:38 PM
    Moderator
  • Can I see your JavaScript code for both the SDK.REST and the method you just used? And possibly a screenshot of your form settings?

    You've been so helpful, I'm horribly sorry that I've taken so much of your time.

    There has to be some minor difference somewhere in the code because I'm still getting the same frustrating error.

    Thursday, June 27, 2013 7:09 PM
  • Here is the script(new_bbb):

    function findTotalInternTests() {
    
        SDK.REST.retrieveMultipleRecords("hbs_zzinterntest", "$select=*", function (results) {
            alert("Success");
        }, function (error) {
            alert(error.message);
        }, function () {
            alert("Complete");
        });
    
    }

    Form properties:

    If you still are having problems to would look at debugging to see if you can't get any more detail. You can hit F12 from IE to get the Developer Tools. From there you can go to the "Scripts" tab and find you script file and set break points. Looking at the "Console" will also show errors. 

    How to Debug JScript in Microsoft Dynamics CRM 2011


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Thursday, June 27, 2013 7:28 PM
    Moderator
  • can u paste a screenshot of the entities listing in the customization window to confirm the schema name of the entity.

    Take an example of account entity, the name is account but schema name is Account and it is the schema name that needs to be mentioned here.

    from the SDK here is the syntax

    var options = "$select=Name,Telephone1&$top=" + number;

    SDK.REST.retrieveMultipleRecords("Account", options, retrieveAccountsCallBack, function (error) { alert(error.message); }, accountsRetrieveComplete);

    Also can you change the second parameter of $select=<attributename> instead of * and see if that returns you any better results.

    HTH

    Sam


    Dynamics CRM MVP | Inogic | http://inogic.blogspot.com| news at inogic dot com

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"


    • Edited by Sam - Inogic Friday, June 28, 2013 4:19 AM updated
    Friday, June 28, 2013 4:18 AM
  • Hello everyone, I have come across the solution to my issue.

    The key was looking at the SDK.REST code. I didn't know what "Error : 0: :" meant, so I found the function that called it. in SDK.REST_errorHandler, the "0" represented the error status code.

    I searched to see what the status code "0", and apparently it means that I was not getting a response at all.

     

    The reason? The Same-Origin Policy

    My CRM form had the header: " http://mycrm.mycompany.com/MYCRM "...

    My server url was returning: " http://mycrm/MYCRM "...

    Though the protocols matched (both http), and both "mycrm" and "mycrm.mycompany.com" both pointed to the same domain, their domain strings were not identical, and this mismatch did not allow data to be retrieved.

    I solved this problem by modifying SDK.REST._getServerURL to return " http://mycrm.mycompany.com/MYCRM ", the url that matched where I called my script.

    • Marked as answer by RyanNHG Wednesday, July 10, 2013 1:54 PM
    Wednesday, July 10, 2013 1:53 PM