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 365 | Integration, Dataverse...
Answered

how to make Xrm.webApi calls response synchronous with other code in a function without using async or await keword

(0) ShareShare
ReportReport
Posted on by 20

Please can someone help me to make a kind of intermediate function or way by which we can call the function having Xrm.webApi call synchronously in any synchronous function without the use of async or await keywords in the existing functions name(in intermediate function async and await will work) like below : 

function show(){

//code

var result=function webapicall();   //  result:undefined  error as  it doesn't wait for api call to finish and move to next statement as the xrm.webApi calls are asynchronous, please help                                                             me in writing such code without async or await keywords which will execute this synchronously.

alert();

//synchronous code

//more code

}

function webapicall()

{

xrm.webApi.retrievemultiplerecords().then(successcallback,error function)

return response;

}

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    I'm afraid you won't be able to do that in a direct way. You will have to use either async/await or promises.

  • ShubhamJain7 Profile Picture
    20 on at

    Thanks Andrew, Could you please share a sample code to handle this with promises as when i am trying with .then() ..i am getting error :cannot read property than of null....

    Below is my code for reference

    function getRecord()
    {

    var b=show().then(function success(result){;
    var fetchXml= "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
    " <entity name='account'>" +
    " <attribute name='name' />" +
    " <attribute name='telephone1' />" +
    " <attribute name='address1_city' />" +
    " <attribute name='primarycontactid' />" +
    " <attribute name='statecode' />" +
    " <filter type='and'>" +
    " <condition attribute='name' operator='like' value='Adventure Works'/>" +
    " </filter>" +
    " </entity>"+
    "</fetch>";
    fetchXml ="?fetchXml=" + encodeURIComponent(fetchXml);
    var account="account";
    var records=parent.Xrm.WebApi.retrieveMultipleRecords(account, fetchXml);
    parent.Xrm.WebApi.retrieveMultipleRecords(account, fetchXml).then( function success(response){

    alert("getRecord");
    alert("namaste"+response.entities[0].name);
    },
    function (error) {
    alert(error.message);
    }
    );
    });

    }

    function show()
    {

    var fetchXml= "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
    " <entity name='account'>" +
    " <attribute name='name' />" +
    " <attribute name='telephone1' />" +
    " <attribute name='address1_city' />" +
    " <attribute name='primarycontactid' />" +
    " <attribute name='statecode' />" +
    " <filter type='and'>" +
    " <condition attribute='name' operator='like' value='Adventure Works'/>" +
    " </filter>" +
    " </entity>"+
    "</fetch>";
    fetchXml ="?fetchXml=" + encodeURIComponent(fetchXml);
    var account="account";
    var response=null;
    parent.Xrm.WebApi.retrieveMultipleRecords(account, fetchXml).then(
    function first(result) {
    alert("show");
    response=result.entities;

    return response;
    },
    function (error) {

    }
    );

    return response;
    }

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    The main reason why your code doesn't work the right way is that the show method has to return the Promise instead of the data itself. I refactored the code of the show function:

    function show() {
    	var fetchXml= "" 
    	" "  
    	" "  
    	" "  
    	" "  
    	" "  
    	" "  
    	" "  
    	" "  
    	" "  
    	" " 
    	"";
    	fetchXml ="?fetchXml="   encodeURIComponent(fetchXml);
    	var account="account";
    
    	return new Promise(function(resolve, reject) {
    		parent.Xrm.WebApi.retrieveMultipleRecords(account, fetchXml).then(
    		function (result) {
    			var response=result.entities;
    			resolve(response);
    		},
    		function (error) {
    			reject(error);
    		});
    
    	});
    }

  • ShubhamJain7 Profile Picture
    20 on at

    Thanks Andrew , it worked but if i want to store the response in the calling variable than is it possible like

    var b=show().then(function success(result){});

    synchronous code

    and rest of the synchronous code...?

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    That won't work as I mentioned in my first reply.

    Put your sync code into success handler. Something like

    show().then(function success(result){

    //your sync code here

    });

  • ShubhamJain7 Profile Picture
    20 on at

    Thanks Andrew

    cheers !!

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 365 | Integration, Dataverse, and general topics

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 76

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans