none
Retrieve field value from a related entity on load

    Question

  • I know there are dozens of other threads out there that ask similar questions and I've tried multiple solutions from those threads but I can't seem to get it to work. I have a decimal value field on the user record for a base commission rate. On a custom entity for commission line item there is a commission rate decimal value field. When I create a new commission rate line item record I want to retrieve the commission base rate value from the owner and have it populate on the new entity record. Below is my code, it seems to run through but I always get the Error Message pop up instead of the field value populated. I'm wondering if the retrieve.responseText could be an issue since it's a decimal not text? But I'm just not sure. Can anyone see whats wrong in the code?

    Thanks,
    Trevor

    if(Xrm.Page.getAttribute("ownerid").getValue() != null)
    {
          var id = Xrm.Page.getAttribute("ownerid").getValue()[0].id;
    
        var _oPath = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc";
        var _retrieve = new XMLHttpRequest();
        _retrieve.open("GET", _oPath + "/User Set(guid'" + id + "')", true);
        _retrieve.setRequestHeader("Accept", "application/json");
        _retrieve.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        _retrieve.onreadystatechange = function () { RetrieveCommissionCallBack(this); };
    
        _retrieve.send();
    }
    }
    
    function RetrieveCommissionCallBack(_retrieve)
    {
        if(_retrieve.readyState == 4)
        {
            if(_retrieve.status == 200)
            {
                var _rate = JSON.parse(_retrieve.responseText).d;
                if(_rate.new_commissionrate.Value != null)
                 {
                         Xrm.Page.getAttribute("new_commissionpercentage").setValue(_rate.new_commissionrate.Value);
    
                 }
     
            }
            else
            {
                window.alert("There was an error retrieving Commission Base Rate Information");
            }
        }
    }

    Wednesday, August 21, 2013 10:12 PM

All replies

  • I see an error on this line:

    _retrieve.open("GET", _oPath + "/User Set(guid'" + id + "')", true);

    It should be SystemUserSet not User Set.

    If you are still getting an error after changing this, _retrieve should contain more detailed information on what the error was. Post this information and I'll be able to help you.

    Thursday, August 22, 2013 1:30 AM
  • Hi,

    You have to provide SystemUserSet. Odata expects the schema name of the fields Not logical name so make sure that while reading value from Odata specify the schema name. Please make sure that you are using proper schema name.

    Please use the below forum replied by me to get the understanding on schema name.

    http://social.microsoft.com/Forums/en-US/0ce10c97-2eef-469a-aa84-7d1fa79d0f17/getting-the-guid-of-a-record-retrieved-by-jscriptodata#23b00c2a-f95a-4e00-8d88-545cce470202

    Hope this helps!


    • Edited by KalimK Thursday, August 22, 2013 5:24 AM
    Thursday, August 22, 2013 5:24 AM
  • Thank you both for your responses. I made the changes both of you suggested. Now when the code runs I don't get any Microsoft Error messages or the Pop up message from the code. However, the field does not populate either? Below is the new code I have.

    function RetrieveCommissionRate() {
    
    if(Xrm.Page.getAttribute("ownerid").getValue() != null)
    {
          var id = Xrm.Page.getAttribute("ownerid").getValue()[0].id;
    
        var _oPath = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc";
        var _retrieve = new XMLHttpRequest();
        _retrieve.open("GET", _oPath + "/SystemUserSet(guid'" + id + "')", true);
        _retrieve.setRequestHeader("Accept", "application/json");
        _retrieve.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        _retrieve.onreadystatechange = function () { RetrieveCommissionCallBack(this); };
    
        _retrieve.send();
    }
    }
    
    function RetrieveCommissionCallBack(_retrieve)
    {
        if(_retrieve.readyState == 4)
        {
            if(_retrieve.status == 200)
            {
                var _rate = JSON.parse(_retrieve.responseText).d;
                if(_rate.new_CommissionRate.Value != null)
                 {
                         Xrm.Page.getAttribute("new_CommissionPercentage").setValue(_rate.new_CommissionRate.Value);
    
                 }
     
            }
            else
            {
                window.alert("There was an error retrieving Commission Base Rate Information");
            }
        }
    }

    Thanks again for your help.
    Friday, August 23, 2013 2:15 PM
  • Hi,

    Change the below line. While Retrieving from Odata Or performing any operation through Odata we need schema name. When we Get/set the field value directly from form we have to provide the logical name. It will be in small case. Use the below Javascript.

    Xrm.Page.getAttribute("new_commissionpercentage").setValue(_rate.new_CommissionRate.Value);

    Thanks!

    Saturday, August 24, 2013 1:00 PM
  • KalimK, thank you for getting back to me. I swapped out that code and I am still having the same problem. When the page loads the CommissionRate value isn't loaded at all into the new form field. I receive no errors though. Do you have any more ideas what the issue might be?

    Thank you,
    Trevor

    Monday, August 26, 2013 1:56 PM
  • I went and found another example of code to mimic and I am getting the same exact issue. The code seems to run but the field isn't populated with the decimal value at all. Nothing happens and there are no error messages that pop up at all either. Here is the second set of code I used to try and make this work. I do the have Json code added to the form as well above this script. The only thing I changed below was the names of the fields. I didn't change anything else. I'm desperately struggling trying to make this work.

    function GetProjectInfo() { 
       var ProjectObject = Xrm.Page.getAttribute("ownerid").getValue();
       var ProjectNoID= ProjectObject[0].id;
       ProjectNoID = ProjectNoID.replace('{', '').replace('}', '');
       if (ProjectNoID != null) {     
          var oDataPath = Xrm.Page.context.getServerUrl() + "/xrmservices/2011/organizationdata.svc";
       var retrieveReq = new XMLHttpRequest();
       retrieveReq.open("GET", oDataPath + "/SystemUserSet(guid'" + ProjectNoID + "')", true);
       retrieveReq.setRequestHeader("Accept", "application/json");
       retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
       retrieveReq.onreadystatechange = function () {
          retrieveProjectReqCallBack(this);
       };
       retrieveReq.send();
    }
    function retrieveProjectReqCallBack(retrieveProjectReq) {
       if(retrieveReq.readyState == 4) {
        if (retrieveReq.status == 200) {
          var retrievedProject = this.parent.JSON.parse(retrieveProjectReq.responseText).d;
          var ProjectName = retrievedProject.new_CommissionRate.value;
          Xrm.Page.getAttribute("new_commissionpercentage").setValue(ProjectName);
       } else {
          alert("Error in Fetching data");
       }
    }
    }
    }

    Monday, August 26, 2013 4:10 PM
  • Hi,

    Please debug the code and Post the error that you are getting. Please put the Try Catch.

    Thanks!

    Monday, August 26, 2013 6:23 PM
  • Hello, thanks again for the reply. I attempted to debug the code following the steps from this website: http://social.technet.microsoft.com/wiki/contents/articles/3256.how-to-debug-jscript-in-microsoft-dynamics-crm-2011.aspx

    When I do that I get an error message that is below. I only get this error message when accessing the CRM from Internet Explorer. I don't get this when I am in Outlook. Even when i attempt to start debugging that error message pops up instead and nothing happens. I'm not sure what I'm being denied access too, I am the system administrator?


    Also I'm a very beginner coder. I haven't had any real experience with Try, Catch. However, I'm going to do some reading up on it. I'm hoping this above error from IE that I wasn't getting before will give some answers though?

    Thanks again!

    [edit] - I did some digging around and found this: http://crm2011wiki.wordpress.com/2012/09/20/dynamics-crm-2011-javascript-access-is-denied-error-with-ajax-call/

    Using that I no longer get the access is denied error but I get the error alert in the code to pop up now. I tried this on both sets of code I have been using and both pop up the error alert message box from the code.


    Monday, August 26, 2013 6:42 PM
  • I figured out the debugger and it looks like the line:

    var _retrieve = new XMLHttpRequest();

    is what is causing the issues. When I run the debugger it gets past the line but _retrieve is never defined it remains {...} then once it gets down the the check the status = 200 it breaks and calls the pop up message box. Any idea how that is fixed? Thanks again!

    Monday, August 26, 2013 9:20 PM
  • I didn't want to start a new thread so I'm hoping someone can see this. When the code below runs everything goes well till checking if the status == 200. I am receive a 404 page not found error. Anyone know what might be causing this?

    function RetrieveCommissionRate() {
    
    if(Xrm.Page.getAttribute("ownerid").getValue() != null)
    {
          var id = Xrm.Page.getAttribute("ownerid").getValue()[0].id;
        var serverUrl = "https://" + window.location.host + "/" + Xrm.Page.context.getOrgUniqueName(); 
        var _oPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
        var _retrieve = new XMLHttpRequest();
        _retrieve.open("GET", _oPath + "/SystemUserSet(guid'" + id + "')", true);
        _retrieve.setRequestHeader("Accept", "application/json");
        _retrieve.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        _retrieve.onreadystatechange = function () { RetrieveCommissionCallBack(this); };
    
        _retrieve.send();
    }
    }
    
    function RetrieveCommissionCallBack(_retrieve)
    {
        if(_retrieve.readyState == 4)
        {
            alert(_retrieve.status);
            if(_retrieve.status == 200)
            {
                var _rate = JSON.parse(_retrieve.responseText).d;
                if(_rate.new_CommissionRate.Value != null)
                 {
                         Xrm.Page.getAttribute("new_commissionpercentage").setValue(_rate.new_CommissionRate.Value);
                         Xrm.Page.getAttribute("new_commissionpercentage").setSubmitMode("always");
                 }
     
            }
            else
            {
                window.alert("There was an error retrieving Commission Base Rate Information");
            }
        }
    }

    Thanks a bunch!

    - Also wanted to add we use on-premise, not sure if that makes a difference?

    I added the new_jQuery. I was missing that but didn't solve the issues still returns 404 page not found. I think this is correct now but not sure...?

    Wednesday, August 28, 2013 2:02 PM

  • With regard to your initial access denied error, this could be because of cross domain scripting.

    Check that the value of serverUrl matches the url that is in the navigation bar on your browser.

    If you try to access https://example.com from a page that has been accessed through http://example.com you will get an access denied error because the browser sees the domains to be different.

    I'd suggest that you use the snippet below to get the correct url under all conditions (including Outlook client)

    if (context.isOutlookClient() && !context.isOutlookOnline()) {
    serverUrl = window.location.protocol + "//" + window.location.host;
    }
    else {
    serverUrl = context.getServerUrl();
    }

    With regard to the 404 not found error use the code below

    var id = Xrm.Page.getAttribute('ownerid').getValue()[0].id.toLowerCase()

    Odata can be finicky about case. 

    Thursday, August 29, 2013 10:08 PM