locked
Retrieve the Lookup.getValue()[0].name value and put it in a text field RRS feed

  • Question

  • I asked in the following thread:

    Populating Fields in Entity using data from Fields of another Entity that has been selected in a filtered Lookup

    and JLattimer answered it perfectly.  I then found out that I had another layer of lookup that I must deal with to get the value I want.

    The following code is the adaptation I've attempted with JLattimer's code.  Everything works, until I get to:

    var retrievedValue = retrieved.results[0].new_WebServer.getValue()[0].name;

    The new_WebServer field on the entity is a lookup field.  I need to obtain the .name of that field.

    //Tie this method to the OnChange event of the lookup you want to retrieve values from
    function ClientSiteOnChange() {
        //This gets the unique identifier of the lookup
        var ClientSite = new Array();
        ClientSite = Xrm.Page.getAttribute("new_clientsite").getValue();
        //Makes sure it is populated before proceeding - else there could be unexpected errors
        if (ClientSite != null) {
            //Use the oData/REST endpoint to query the field(s) we need based on the filter criteria (id of the lookup)
            var retrieveReq = new XMLHttpRequest();
            var ODataPath = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/new_clientsiteSet?$select=new_WebServer&$filter=new_clientsiteId eq guid'" + ClientSite[0].id + "'";
            retrieveReq.open("GET", ODataPath, true);
            retrieveReq.setRequestHeader("Accept", "application/json");
            retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            retrieveReq.onreadystatechange = function () {
                getLookupFieldData(this);
            };
            retrieveReq.send();
        }
    }
    function getLookupFieldData(retrieveReq) {
        //If/When the request returns a result
        if (retrieveReq.readyState == 4) {
            if (retrieveReq.status == 200) {
                //Parse the result and set the field value on the form
                var retrieved = JSON.parse(retrieveReq.responseText).d;
    			var retrievedValue = retrieved.results[0].new_WebServer.getValue()[0].name;
                Xrm.Page.getAttribute("new_webserver").setValue(retrievedValue);
            } else {
                //error
            }
        }
    }

    Overall I'm perplexed that the CRM API doesn't allow easy access from entity to entity, and grabbing values.  Am I completely missing something?  I feel as if I'm missing something, as I've thought you should be able to easily grab data from all of these entities, without needing to deploy 3rd party code such as the JSON function.

    Thursday, August 23, 2012 4:28 AM

Answers

  • Have a look at this - verify your Client Site lookup field actually has the Name ending in 'id' when you look at the properties, also changed the URL and removed a question mark that wasn't needed and then remove the '.Name' when setting the retrievedValue variable.

    You might want to check out this wiki article when you have moment - it might be of some use in the future.

    How to Debug JScript in Microsoft Dynamics CRM 2011

    function ClientSiteOnChange() {
        //This gets the unique identifier of the lookup
        var serverUrl = "http://" + window.location.host + "/" + Xrm.Page.context.getOrgUniqueName();
        var ClientSite = Xrm.Page.getAttribute("new_clientsiteid").getValue();
        var ClientSiteid = ClientSite[0].id;
        //Makes sure it is populated before proceeding - else there could be unexpected errors
        if (ClientSite != null) {
            //Use the oData/REST endpoint to query the field(s) we need based on the filter criteria (id of the lookup)
            var oDataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
            var retrieveReq = new XMLHttpRequest();
            var Odata = oDataPath + "/new_clientsiteSet?$select=new_WebServer&$filter=new_clientsiteId eq guid'" + ClientSiteid + "'";
    
            retrieveReq.open("GET", Odata, false);
            retrieveReq.setRequestHeader("Accept", "application/json");
            retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            retrieveReq.onreadystatechange = function () {
                getLookupFieldData(this);
            };
            retrieveReq.send();
        }
    }
    
    function getLookupFieldData(retrieveReq) {
        //Parse the result and set the field value on the form
        var retrieved = this.parent.JSON.parse(retrieveReq.responseText).d;
        var retrievedValue = retrieved.results[0].new_WebServer;
        Xrm.Page.getAttribute("new_webserver").setValue(retrievedValue);
    }
    

    Jason Lattimer

    Monday, August 27, 2012 3:17 AM
    Moderator

All replies

  • Hi Jarthurs,

      Try this Code:

    function ClientSiteOnChange() { //This gets the unique identifier of the lookup var ClientSite = new Array(); ClientSite = Xrm.Page.getAttribute("new_clientsite").getValue(); //Makes sure it is populated before proceeding - else there could be unexpected errors if (ClientSite != null) { //Use the oData/REST endpoint to query the field(s) we need based on the filter criteria (id of the lookup) var retrieveReq = new XMLHttpRequest(); var ODataPath = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/new_clientsiteSet?$select=new_WebServer&$filter=new_clientsiteId eq guid'" + ClientSite[0].id + "'"; retrieveReq.open("GET", ODataPath, true); retrieveReq.setRequestHeader("Accept", "application/json"); retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8"); retrieveReq.onreadystatechange = function () { getLookupFieldData(this); }; retrieveReq.send(); } } function getLookupFieldData(retrieveReq) { //If/When the request returns a result if (retrieveReq.readyState == 4) { if (retrieveReq.status == 200) { //Parse the result and set the field value on the form var retrieved = JSON.parse(retrieveReq.responseText).d; var retrievedValue = retrieved.results[0].new_WebServer.Name;

    var id=retrieved.results[0].new_WebServer.Id;

    var value = new Array();
    value[0] = new Object();
    value[0].id = id;
    value[0].name = retrievedValue;
    value[0].entityType ="Entity Name";(//Give the Entity name of that lookup)


    Xrm.Page.getAttribute("new_webserver").setValue(value); } else { //error } } }


    Naren

    Thursday, August 23, 2012 5:05 AM
  • Naren, thank you for your reply.

    var retrievedValue = retrieved.results[0].new_WebServer.Name;

    var id=retrieved.results[0].new_WebServer.Id;

    If I try either of the above, I get an error stating that the object does not support the method.  I wish that first statement would work, then my problems would be over.

    I'm finding CRM 2011 to be case sensitive, and in previous code snippets I've read and seen work, the methods have a proper case of

    name and id

    Thursday, August 23, 2012 5:15 AM
  • Try this,

    function ClientSiteOnChange() { //This gets the unique identifier of the lookup

    var serverUrl = "http://" + window.location.host + "/" + Xrm.Page.context.getOrgUniqueName(); var ClientSite = Xrm.Page.getAttribute("new_clientsite").getValue();

    var ClientSiteid = ClientSite[0].id; //Makes sure it is populated before proceeding - else there could be unexpected errors if (ClientSite != null) { //Use the oData/REST endpoint to query the field(s) we need based on the filter criteria (id of the lookup)

    var oDataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; var retrieveReq = new XMLHttpRequest();

    var Odata = oDataPath + "/?new_clientsiteSet?$select=new_WebServer&$filter=new_clientsiteId eq guid'" + ClientSiteid + "'";

    retrieveReq.open("GET", Odata, false);
        retrieveReq.setRequestHeader("Accept", "application/json");
        retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        retrieveReq.onreadystatechange = function () { getLookupFieldData(this); };
        retrieveReq.send(); 

    } function getLookupFieldData(retrieveReq)

    { //Parse the result and set the field value on the form var retrieved = this.parent.JSON.parse(retrieveReq.responseText).d; var retrievedValue = retrieved.results[0].new_WebServer.Name;

    var id=retrieved.results[0].new_WebServer.Id;

    var value = new Array();
    value[0] = new Object();
    value[0].id = id;
    value[0].name = retrievedValue;
    value[0].entityType ="Entity Name";(//Give the Entity name of that lookup)


    Xrm.Page.getAttribute("new_webserver").setValue(value);

    }


    Naren



    • Edited by Naren MN Thursday, August 23, 2012 5:27 AM
    Thursday, August 23, 2012 5:24 AM
  • Naren,

    Thank you for your followup.

    The code isn't throwing any errors, but it isn't populating the field either.

    Looking at your approach, you created the object "value".  The destination field, is a text field, while the source field is a lookup.

    I changed the code, to be:

    Xrm.Page.getAttribute("new_webserver").setValue(retrievedValue);

    It still doesn't populate the destination text field.

    I am only wanting the name of the entity selected in the Lookup field called new_WebServer.

    Thursday, August 23, 2012 2:48 PM
  • Have a look at this - verify your Client Site lookup field actually has the Name ending in 'id' when you look at the properties, also changed the URL and removed a question mark that wasn't needed and then remove the '.Name' when setting the retrievedValue variable.

    You might want to check out this wiki article when you have moment - it might be of some use in the future.

    How to Debug JScript in Microsoft Dynamics CRM 2011

    function ClientSiteOnChange() {
        //This gets the unique identifier of the lookup
        var serverUrl = "http://" + window.location.host + "/" + Xrm.Page.context.getOrgUniqueName();
        var ClientSite = Xrm.Page.getAttribute("new_clientsiteid").getValue();
        var ClientSiteid = ClientSite[0].id;
        //Makes sure it is populated before proceeding - else there could be unexpected errors
        if (ClientSite != null) {
            //Use the oData/REST endpoint to query the field(s) we need based on the filter criteria (id of the lookup)
            var oDataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
            var retrieveReq = new XMLHttpRequest();
            var Odata = oDataPath + "/new_clientsiteSet?$select=new_WebServer&$filter=new_clientsiteId eq guid'" + ClientSiteid + "'";
    
            retrieveReq.open("GET", Odata, false);
            retrieveReq.setRequestHeader("Accept", "application/json");
            retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            retrieveReq.onreadystatechange = function () {
                getLookupFieldData(this);
            };
            retrieveReq.send();
        }
    }
    
    function getLookupFieldData(retrieveReq) {
        //Parse the result and set the field value on the form
        var retrieved = this.parent.JSON.parse(retrieveReq.responseText).d;
        var retrievedValue = retrieved.results[0].new_WebServer;
        Xrm.Page.getAttribute("new_webserver").setValue(retrievedValue);
    }
    

    Jason Lattimer

    Monday, August 27, 2012 3:17 AM
    Moderator