Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

JavaScript retrieve record via Web API - wait for function to return value

(0) ShareShare
ReportReport
Posted on by 1,797

Hi, 

How would I go about ensuring teamResult waits until checkTeam has returned a value in the following code? Writing to the console, I have established the execution order is not as I would hope. I have had success writing this as a single function, but ideally I would like to reuse the checkTeam function. 

Console:

undefined
7db988a2-f87c-e911-a98a-00224800c5df
true

Code:

function lockDueDate(executionContext) {

"use strict";

    var formContext = executionContext.getFormContext(); 
    var globalContext = Xrm.Utility.getGlobalContext();
    var organizationName = globalContext.getClientUrl();

    if (organizationName.indexOf("****") >= 0) {
        var team = "****";
    } else if (organizationName.indexOf("****") >= 0) {
        var team = "****";
    } else if (organizationName.indexOf("****") >= 0) {
        var team = "****";
    } else if (organizationName.indexOf("****") >= 0) {
		var team = "****";
	} else { 
	    return;
	}

	//if member of team lock due date
    var dueDate = formContext.getControl("header_scheduledend");
    var teamResult = checkTeam(team);

    if (teamResult === false) {
        if (dueDate != null) {
            dueDate.setDisabled(true);
        } else {
            return;
        }
    }
}

//Check to see if user is member of team
function checkTeam(teamId) {
	
    var context = Xrm.Utility.getGlobalContext();
    var clientURL = context.getClientUrl();
    var req = new XMLHttpRequest();
    var userId = context.userSettings.userId.substr(1, 36);
	
    Xrm.WebApi.online.retrieveMultipleRecords("teammembership", "?$select=teammembershipid&$filter=systemuserid eq " + userId + " and  teamid eq " + teamId +" ").then(
    function success(results) {
        for (var i = 0; i < results.entities.length; i++) {
		var membership = results.entities[i]["teammembershipid"];
			if (membership.length > 0) {
				return true; 
			} 	
        }
   return false;		
    },
    function(error) {
        Xrm.Utility.alertDialog(error.message);
    }
  );
}


	

*This post is locked for comments

  • Verified answer
    Abhishek Gupta Profile Picture
    Abhishek Gupta 2,001 on at
    RE: JavaScript retrieve record via Web API - wait for function to return value

    HI,

    There is no direct way to achieve synchronous call for Xrm.WebApi.

    Is it possible to move your below section of code into Xrm.Webapi call, as shown below:

    Xrm.WebApi.online.retrieveMultipleRecords("teammembership", "?$select=teammembershipid&$filter=systemuserid eq " + userId + " and  teamid eq " + teamId +" ").then(

       function success(results) {

           for (var i = 0; i < results.entities.length; i++) {

    var membership = results.entities[i]["teammembershipid"];

    if (membership.length > 0) {

                           if (teamResult === false) {

                              if (dueDate != null) {

                                 dueDate.setDisabled(true);

                             } else {

                                  return;

                               }

    }

           }

    Now if Api call return with success then only your code  dueDate.setDisabled(true);  works.

    Thank you

    Abhishek

    If found useful, please mark the answer as verified

  • Verified answer
    LMP Profile Picture
    LMP 678 on at
    RE: JavaScript retrieve record via Web API - wait for function to return value

    Hi J M,

    I believe it's not possible to make this call synchronous. You can try using nested success callback instead. Please check the links below.

    github.com/.../150

    arundynamix.blogspot.com/.../xrmwebapi-call-synchronous-in-v9.html

    blog.thenetw.org/.../making-xrm-webapi-retrieverecord-synchronous-calls-in-common-data-service-d365-fo-ce

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,401 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans