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 :
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,216 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

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 73 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 43 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans