Skip to main content

Notifications

Announcements

No record found.

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

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;

}

  • ShubhamJain7 Profile Picture
    ShubhamJain7 20 on at
    RE: how to make Xrm.webApi calls response synchronous with other code in a function without using async or await keword

    Thanks Andrew

    cheers !!

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: how to make Xrm.webApi calls response synchronous with other code in a function without using async or await keword

    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
    ShubhamJain7 20 on at
    RE: how to make Xrm.webApi calls response synchronous with other code in a function without using async or await keword

    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...?

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: how to make Xrm.webApi calls response synchronous with other code in a function without using async or await keword

    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
    ShubhamJain7 20 on at
    RE: how to make Xrm.webApi calls response synchronous with other code in a function without using async or await keword

    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;
    }

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: how to make Xrm.webApi calls response synchronous with other code in a function without using async or await keword

    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.

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

Product updates

Dynamics 365 release plans