web
You’re offline. This is a read only version of the page.
close
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,799

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

I have the same question (0)
  • Verified answer
    LMP Profile Picture
    680 on at

    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

  • Verified answer
    Abhishek Gupta Profile Picture
    2,003 on at

    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans