Search code examples
javascriptdynamics-crm-2015

Returning the return value from inner function to parent function- Javascript, Dynamics crm


Will the below code return the boolean value from the innner function to the parent function displayButton()? The parent function is called on click of a button in dynamics CRM. The function should return a boolean value depending if the a case is selected and the selected is active or resolve.

   //function called on click of a button in ms crm. 
    function displayButton()
    {
        var Obj = parent.Xrm.Page.getAttribute("regardingobjectid");
        var ObjValue = Obj.getValue();
        //parent.Xrm.Utility.alertDialog(" Value: " + ObjValue);
        if (ObjValue == null)
            return false;
        //else
        //    parent.Xrm.Utility.alertDialog(" Hi");

        var EntityType = ObjValue[0].entityType;

        var Guid = ObjValue[0].id;
        var id = Guid.slice(1, -1);
        //parent.Xrm.Utility.alertDialog(" Guid: " + id);

//Checking if regarding field is selected a case lookup value
        if (EntityType == "incident")
        {
            var req = new XMLHttpRequest();
            req.open("GET", parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/incidents(" + id + ")?$select=statecode", 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)
                    {

                        debugger;
                        var result = JSON.parse(this.response);

//checking if selected case is active or resolved.
                        var statecode = result["statecode"];

                        var statecode_formatted = result["[email protected]"];
                        if (statecode_formatted == "Active") {
                            return true;

                        }
                        else if (statecode_formatted == "Resolved")
                            return false;
                        else {
                            return false;
                        }
                    }
                    else
                    {
                        parent.Xrm.Utility.alertDialog("Zero");
                    }


                }

            };
            req.send();

        }
        else {
            return false;
        }


    }

Solution

  • No. If you want to access the value returned by your asynchronous XmlHttpRequest, either you'll need to put your logic in the if (this.status === 200) scope or provide a callback.

    Your XMLHttpRequest is asynchronous because of the true parameter in this line:

    req.open("GET", parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/incidents(" + id + ")?$select=statecode", true);
    

    To provide a callback, separate out your code into two functions:

    function getCaseState(id, callback) {
        var req = new XMLHttpRequest();
        req.open("GET", parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/incidents(" + id + ")?$select=statecode", 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 result = JSON.parse(this.response);
                    var statecode = result["statecode"];
                    var statecode_formatted = result["[email protected]"];
    
                    callback(statecode_formatted);
                }
            }
        };
        req.send();
    }
    

    Then call getCaseState, passing it the id of your incident and a function to call once the request is ready:

    // called on click of a button in CRM. 
    function displayButton() {
        var regarding = parent.Xrm.Page.getAttribute("regardingobjectid").getValue();
        var entityType = regarding[0].entityType;
        var regardingId = regarding[0].id.slice(1, -1);
    
        // Check if regarding is an active case.
        if (entityType == "incident") {
            getCaseState(regardingId, function(state) {
                var isActive = state === "Active";
    
                if (isActive) {
                    // TODO
                }
            });
        }
    }
    

    The function passed in the code above is anonymous -- you should separate it out further and name it.