Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Web-API - Getting return value function

Posted on by Microsoft Employee

I'm attempting to use a variable to hold the results of a web-api call. However, it keeps coming back as undefined .

When I debug the web-api call itself I do get an expected return value. Basically I want the variable responseCancellationPacket to be able to access the return ApprovalState property/variable in the web-api.  It should either be 0 (if the query in the web-api call does not return a hit) or an actual value from the CRM record. I must be missing something perhaps in the variable declaration that calls the web-api function?

First I get the Guid of the record

Then I declare a variable which calls a function in a utility file that holds various web-api calls. In this case I pass the entity type name and Guid

var entityId = Xrm.Page.data.entity.getId();
 var responseCancellationPacket = WebApiHelper.DocumentationFMA.GetCancellationPacketInfo("entityName", entityId);

The actual web-api call is as follows:

GetCancellationPacketInfo: function (entityType, entityId) {
        entityId = entityId.substr(1, 36);
        var approvalstate = 0;
        var entityName = Xrm.Page.data.entity.getEntityName();
        if (entityName == entityType) {
            var clientURL = Xrm.Page.context.getClientUrl();
            var req = new XMLHttpRequest();
            var query = "/api/data/v8.0/aprv_documentations?$select=aprv_approvalstate&$filter=aprv_included eq 3 and  _aprv_projectid_value eq " + entityId + "";
            req.open("GET", encodeURI(clientURL + query), 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.onreadystatechange = function () {
                if (this.readyState === 4) {
                    req.onreadystatechange = null;
                    if (this.status === 200) {
                        var results = JSON.parse(this.response);
                        if (results.value.length > 0) {
                           
                            for (var i = 0; i < results.value.length; i++) {
                                var ApprovalState = results.value[i]["aprv_approvalstate"];
                                return ApprovalState = approvalstate;
                            }
                        }
                        return ApprovalState = approvalstate;
                    } else {
                        Xrm.Utility.alertDialog(this.statusText);
                    }
                }
            };
            req.send();

        }

    },

Any insight is appreciated.

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Web-API - Getting return value function

    will try but I thought 'false' was not a good idea that we wanted it asynchronous?

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Web-API - Getting return value function

    Hi ,

    Try to make the post synchronous -

     req.open("GET", encodeURI(clientURL + query), false);

    Let me know .

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Web-API - Getting return value function

    Thanks - still have a couple of issues:

    Even though I can set a breakpoint in the web-api call to where a hit on the query is met and the variable approvalstate is other than zero , the variable that originally calls this function (see above responseCancellationPacket) always returns a value of zero.  

    Secondly, what I would like to be able to do is use the responseCancellationPacket variable as a kind of object so that I can use logic like 'if (responseCancellationPacket.ApprovalState = 1 ....  

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Web-API - Getting return value function

    Hi,

    Try with this  -

            GetCancellationPacketInfo: function (entityType, entityId) {
                entityId = entityId.substr(1, 36);
                var approvalstate = 0;
                var entityName = Xrm.Page.data.entity.getEntityName();
                if (entityName == entityType) {
                    var clientURL = Xrm.Page.context.getClientUrl();
                    var req = new XMLHttpRequest();
                    var query = "/api/data/v8.0/aprv_documentations?$select=aprv_approvalstate&$filter=aprv_included eq 3 and  _aprv_projectid_value eq " + entityId + "";
                    req.open("GET", encodeURI(clientURL + query), true); // Try to make it false if true will not work
                    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.onreadystatechange = function () {
                        if (this.readyState === 4) {
                            req.onreadystatechange = null;
                            if (this.status === 200) {
                                var results = JSON.parse(this.response);
                                if (results.value.length > 0) {
                                
                                    for (var i = 0; i < results.value.length; i++) {
                                        approvalstate = results.value[i]["aprv_approvalstate"]; //Removed var as already declared
                                        // ApprovalState = approvalstate; // You have already declared the variable so no need to declare again just assigned 
                                    }
                                }
                                //return ApprovalState = approvalstate; //Already assigned so commented this code
                                return approvalstate; 
                            } else { 
                                Xrm.Utility.alertDialog(this.statusText);
                                return approvalstate;
                            }                      
                        }
                    };
                    req.send();
                    return approvalstate;
                }
                return approvalstate;
    
            },

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans