web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Access variable from retrieveMultipleRecords().then and use it outside the function

(0) ShareShare
ReportReport
Posted on by 45

Hello I'm using retrieveMultipleRecords to get some records from another table I'm using the data I'm getting and putting it into a variable I want to access that variable outside of the retrieveMultipleRecords function but I cant

this is my js code

    window.parent.Xrm.WebApi.retrieveMultipleRecords("zs_resultat", fetchXMLResultats).then(

                                  function success(resultresultats) {

                                    for (var q = 0; q < resultresultats.entities.length; q++){


                                        json+='        {\n            "level":'+resultresultats.entities[q].zs_numero+',\n'
                                        json+='           "min":'+resultresultats.entities[q].zs_bornemin+',\n'
                                        json+='           "max":'+resultresultats.entities[q].zs_bornemax+',\n'
                                        json+='            "label": {\n'+resultresultats.entities[q].zs_jsonlabelresultat+'\n            },'
                                        json+='            "description": {\n'+resultresultats.entities[q].zs_jsondescresultat+'\n            },\n        }'
                                        if(resultresultats.entities.length-q>1){
                                            json+=','
                                        }


                                    }

                                    
                                  },
                                  function (error) {
                                      console.log(error.message);
                                      // end resultat
                                  }
                                );


////I want to use the variable 'json' here please how can I do that

I have the same question (0)
  • Suggested answer
    meelamri Profile Picture
    13,218 User Group Leader on at

    Hi Omar,

    You can use this code:

    var resultresultats  = await window.parent.Xrm.WebApi.retrieveMultipleRecords (....)

    for (var q = 0; q < resultresultats.entities.length; q++){

        ......

    }

  • Omar Bouattour Profile Picture
    45 on at

    And how do i get my json variable outside that? because i need the await to be inside an async function and i want to access the variable outside that function

  • Suggested answer
    Pradeep Rai Profile Picture
    5,489 Moderator on at

    Hi,

    Please check below code snippet.

    //define global variable
    var jsonData=null;
    
    // form onload method.
    async function executeOnLoad() {
       
       await getAccountData();
       printAccountData();
    }
    
    async function getAccountData() {
       jsonData = await Xrm.WebApi.retrieveMultipleRecords("account", "?$select=name");
       
    }
    
    function printAccountData() {
       
       debugger;
       console.log("print data");
       console.log(jsonData);
    
    
    }
    
    executeOnLoad()



    Output:

    pastedimage1638464333000v2.png

    Thanks,
    Pradeep.

    Please mark this as VERIFIED if it helps

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 69 Most Valuable Professional

#2
ManoVerse Profile Picture

ManoVerse 55 Super User 2026 Season 1

#3
11manish Profile Picture

11manish 37

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans