locked
JScript gets unloaded RRS feed

  • Question

  • Hi, I use the SDK.REST library which comes with CRM sdk to perform REST operations. Many users reported a problem, JS error which says: Can't execute code from a freed script. I found the line of code, and it is a callback function call after retrieveRecord from the library. I found that this kind of problem is related to script which gets unloaded for some reason. Users have allways more than one window opened and I can see from IE developer tools that a SDK.REST library gets loaded more that once (I gues because of a ribbon buttons it is referenced from also). I am not sure about the exact reason of the problem, I didn't manage to reproduce the problem on my machine yet. Any suggestion is welcome.

    Thanks in advance.


    Zarko

    Wednesday, September 11, 2013 10:02 AM

All replies

  • Is your environment is updated with latest rollup ??

    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, September 11, 2013 10:40 AM
    Moderator
  • No, it is on UR 11. Could that be the problem? Is there a documentet fix for that?

    Zarko

    Wednesday, September 11, 2013 10:42 AM
  • This sounds like it an asynchronous call is still being processed and the rest of the script has completed. Example - you are doing a SDK.REST.retrieveRecord during the OnSave event of a form - by default this call is being done asynchronously so the retrieve request is sent and the rest of the script continues processing and sounds like it might be completing before the callback is being fired. 

    The fix would probably be to make the request synchronously to make sure the callback is fired before continuing with the remainder of the code. The SDK.REST library doesn't provide a mechanism to make the request in this manner. So your options might be to extend the existing SDK.REST library and add an argument to the retrieveRecord method to specify sync. or async or not use that library and create your own XMLHttpRequest.

    Example:

    var contact = Xrm.Page.getAttribute("contactid").getValue();
    if (contactid === null)
        return;
    
    var serverUrl = Xrm.Page.context.getClientUrl();
    var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/ContactSet(guid'" + contact[0].id + "')?$select=MobilePhone";
    
    var req = new XMLHttpRequest();
    req.open("GET", oDataSelect, false); //false = sync & true = async
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    req.onreadystatechange = function () {
        if (req.readyState === 4) {
            if (req.status === 200) {
                var retrieved = JSON.parse(req.responseText).d;
                Xrm.Page.getAttribute("new_phone").setValue(retrieved.results[0].MobilePhone);
            }
        }
    };
    req.send();


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Wednesday, September 11, 2013 1:02 PM
    Moderator
  • Hi, thanks for your reply. The code which starts the asynchronous call is running OnLoad of a custom entity. If the OnLoad script completed before the callback, then I should never use asynchronous call from the OnLoad script, right? I didn't mention that the callback function is defined inline like this:

    SDK.REST.retrieveRecord(userId, "SystemUser", null, null,
         function (user) {...},
         errorHandler
       );

    Could that be the problem? And why doesn't this happen every time? I haven't been able to reproduce the problem on my pc.

    Thanks in advance


    Zarko

    Wednesday, September 11, 2013 1:15 PM
  • Correct - the error does imply the rest of the script completed before the callback. Depending on the latency to the server, data returned, etc... this could show up as an intermittent issue. 

    I would try making a synchronous call to see if that eliminates the error.


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Wednesday, September 11, 2013 2:10 PM
    Moderator
  • OK, I will try that, and mark it as the answer if the problem gets solved.


    Zarko

    Thursday, September 12, 2013 7:30 AM