Search code examples
javascriptdynamics-crm

not able to get result length from ms crm web api call output in js


Below is my code snippet. I am trying to fetch all the Referenced Entities from ManyToOne Relationship of Annotation entity. In the result, I am able to get the object but when I'm trying to get the length of it, its giving "undefined". Please provide your valuable suggestions on this and how can we assign the Referenced Entity to a variable from result.

OR is there any possibility to retrieve all the entities that are associated with Annotation entity, using Web API Call ( Dynamics 365).

function fetchIt()
{
    var req = new XMLHttpRequest();
    var webAPICall = Xrm.Page.context.getClientUrl() + "/api/data/v8.2/EntityDefinitions(LogicalName='annotation')?$select=LogicalName&$expand=ManyToOneRelationships($select=ReferencedEntity)";
    req.open("GET", webAPICall, true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function ()
    {
        if (this.readyState === 4)
        {
            req.onreadystatechange = null;
            if (this.status === 200) {
                
                var results = JSON.parse(this.response);
                alert("results.valuelength: " +results.value.length);
                for (var i = 0; i < results.value.length; i++)
                {
                    var referencedEntity = results.value[i]["ReferencedEntity"];
                }
            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }
    };
    }


Solution

  • You have two problems

    1. You never send your request. You're missing req.send()
    2. It won't be results.value it will be results.ManyToOneRelationships

    Then it will work