Search code examples
javascriptdynamics-crmmicrosoft-dynamicsdynamics-crm-2016

crm 2016 javaScript Get lookup name by Fetch


i try to get a Lookup field from Entity (not matter the type) with fetchXml but i don't get in the result the text of the lookup;

for example (account entity, owner field):

var fetchXml = "<fetch mapping='logical'>" +
            "<entity name='account'>" +
                "<all-attributes />" +
                "<filter>" +
                    "<condition attribute='accountid' operator='eq' value='{475B158C-541C-E511-80D3-3863BB347BA8}' />" +
                "</filter>" +
            "</entity></fetch>";

var fetchResult = XrmServiceToolkit.Soap.Fetch(fetchXml);
var owner = fetchResult[0].attributes["ownerid"];

the result [logicalName missing]:

enter image description here

i found a solution to use linkEntity but it's not a good solution for me because i need a generic function

if i get the field directly from the form i get all the information: Xrm.Page.getAttribute("ownerid").getValue()[0] enter image description here


Solution

  • I see you're using XrmServiceToolkit and CRM 2016. I had this same problem with this combination of tools

    I found that someone else (not me) had already reported and fixed this. Refer to this bug

    It seems like the XML being returned from CRM has changed to include 2 new nodes and that this is breaking the way the XrmServiceToolkit is reading the option set values.

    The bug report includes a possible fix. I'm including it here for future reference

    The original XrmServiceToolkit has this code

    case "a:EntityReference":
        entRef = new xrmEntityReference();
        entRef.type = sType.replace('a:', '');
        var oChildNodes = tempNode.childNodes;
        entRef.id = getNodeText(oChildNodes[0]);
        entRef.logicalName = getNodeText(oChildNodes[1]);
        entRef.name = getNodeText(oChildNodes[2]);
        obj[sKey] = entRef;
        break;
    

    You can see it is referring explicitly to oChildNodes[0]; oChildNodes[1]; and oChildNodes[2] - this is causing the bug you've encountered.

    The updated code is:

    case "a:EntityReference":
        entRef = new xrmEntityReference();
        entRef.type = sType.replace('a:', '');
        var oChildNodes = tempNode.childNodes;
        for (var n = 0; n < oChildNodes.length; n++) {
            var currentNodeName = oChildNodes[n].nodeName;
            switch (currentNodeName) {
                case "a:Id":
                    entRef.id = getNodeText(oChildNodes[n]);
                    break;
                case "a:Name":
                    entRef.name = getNodeText(oChildNodes[n]);
                    break;
                case "a:LogicalName":
                    entRef.logicalName = getNodeText(oChildNodes[n]);
                    break;
            }
        }
        obj[sKey] = entRef;
        break;