locked
Strange and Inconsistent Behavior with JavaScript setValue of a GUID into a Textbox RRS feed

  • Question

  • I am encountering an extremely strange behavior when I try to set the GUID that I retrieve from a related entity via Lookup in a text box using Javascript that triggers OnChange.

    If I use the Script below with the alert, it shows the GUID in the alert, and successfully sets the value of the "new_licenseeguid" field to the retrieved GUID. - works perfect.

    function SetGUIDinField() {
        var getGuid = Xrm.Page.getAttribute("new_licensefrmlicensee").getValue()[0].id;  
        alert(getGuid);    Xrm.Page.getAttribute("new_licenseeguid").setValue(getGuid);
    }

    Problem is, I need to eliminate the alert but as soon as I remove the alert code, re-publish the solution, and then reload  the page and refresh my browser, when I trigger the on change, nothing happens, NOT EVEN AN ERROR!

    function SetGUIDinField() {
        var getGuid = Xrm.Page.getAttribute("new_licensefrmlicensee").getValue()[0].id;  
    Xrm.Page.getAttribute("new_licenseeguid").setValue(getGuid);
    }

    You can see below, no errors and the field that was previously populated with the GUID remains blank.

    I have been researching this for over 2 days now and have come up empty.

    Anyone see this before and have any ideas on how I can still get the text box populated while removing the alert?




    • Edited by ACECORP Saturday, October 17, 2015 4:28 PM
    Saturday, October 17, 2015 4:17 PM

Answers

All replies

  • when you have this kind of issues you should debug the javascript (here a method: http://social.technet.microsoft.com/wiki/contents/articles/3256.how-to-debug-jscript-in-microsoft-dynamics-crm-2011.aspx)

    however I will rewrite your code in this way:

    function SetGUIDinField() {
    var licenseValue = Xrm.Page.getAttribute("new_licensefrmlicensee").getValue();
    if (licenseValue != null) {
        Xrm.Page.getAttribute("new_licenseeguid").setValue(licenseValue[0].id);
    } else {
        Xrm.Page.getAttribute("new_licenseeguid").setValue(null);
    }
    }
    in order to handle when the lookup is empty


    My blog: www.crmanswers.net - CRM Theme Generator

    Saturday, October 17, 2015 4:40 PM
  • Thanks, I will check that debugging link out and see what I can find. 

    The lookup will never be empty, except when a new license is being created, but as soon as a licensee gets selected, that should trigger the on-change event which should insert the value into the textbox.

    I'd think that if the alert works and the .setValue works when both the alert and the .setValue are present, then the .setValue should also work even with the alert gone, but maybe there is something strange  going on with invisible text or some other anomaly.

    Hopefully that debug info link will allow me to zero in on the problem. I tried the normal F12 internet explorer script debugger already but could find no problems there. It acts as if the GUID was properly set into the text box.

    Real odd.

    Thanks for pointing me towards this other way to debug - I'll update this post with my findings!

    Saturday, October 17, 2015 4:50 PM
  • Any suggestions on what specifically I should be looking for in the VS debugger that may indicate why "Xrm.Page.getAttribute("new_licenseeguid").setValue(licenseValue[0].id);" is not behaving as expected?

    I was able to get the debugger in place and I am able to confirm that the value of licenseValue[0].id is the expected GUID string.

    Since alert(licenseValue[0].id) always displayed the expected value without incident, I know I have the value.   

    However, just as a precaution, I changed the last line to setValue to Mickey Mouse should something odd happen. 

    function SetGUIDinField() {
    var licenseValue = Xrm.Page.getAttribute("new_licensefrmlicensee").getValue();
    if (licenseValue != null) {
        Xrm.Page.getAttribute("new_licenseeguid").setValue(licenseValue[0].id);
    debugger; 
    } else {
        Xrm.Page.getAttribute("new_licenseeguid").setValue("Mickey Mouse");
    }
    }

    Is there somewhere specific that shows if "Xrm.Page.getAttribute("new_licenseeguid").setValue(licenseValue[0].id);" actually executed and what the result was? 

    When I hit F5 to step through the debugger to the end, no exceptions occur, but also setValue(licenseValue[0].id) doesn't put anything in the text box, and Mickey Mouse doesn't appear there either. 

    Saturday, October 17, 2015 7:29 PM
  • Problem solved - see resolution here https://community.dynamics.com/crm/f/117/t/179113
    • Marked as answer by ACECORP Monday, October 19, 2015 2:02 PM
    Monday, October 19, 2015 2:02 PM