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...
Suggested Answer

Call external URL fetch data

(0) ShareShare
ReportReport
Posted on by

Hi Experts,

I am writing JavaScript to fetch data from a logic app endpoint. The code like below:

 

function DynamicFlyoutMenu(commandProperties) {

    debugger;
    var templates = []; //global templates variable

    const requestOptions = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ get_templates: 'Yes' })
    };

    fetch('prod-16.canadaeast.logic.azure.com:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'requestOptions)
        .then(response => response.json())
        .then(data => {

            alert(JSON.stringify(data));
            for (const key in data) {
                console.log(`${JSON.stringify(data[key])}`//access values

                for (var i in data)
                    templates.push([data[i]]);
                templates = templates[0][0];
            }
        })

==========================================================
I need to get the template array, but when I run the script, the templates still empty, but the alert display the data like below

{"all-template-data":
[
{"templateName":"Temp001.docx","templateId":"DOC001"},
{"templateName":"Temp002.docx","templateId":"DOC002"},
{"templateName":"Temp003.docx","templateId":"DOC003"}
]
}
Can you help to suggest correct code? Thanks.
I have the same question (0)
  • Suggested answer
    Steve Zhao2 Profile Picture
    on at

    Hi ALL365,

    Your code is correct. You say that the templates still empty. S0, where do you console the templates, inside the then() or outside?

    if you console the templates array inside the then() like this:

    function DynamicFlyoutMenu(commandProperties) {
        debugger;
        var templates = []; //global templates variable
        const requestOptions = {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ get_templates: 'Yes' })
        };
        fetch('prod-16.canadaeast.logic.azure.com:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'requestOptions)
            .then(response => response.json())
            .then(data => {
                alert(JSON.stringify(data));
                for (const key in data) {
                    console.log(`${JSON.stringify(data[key])}`//access values
                    for (var i in data)
                        templates.push([data[i]]);
                    templates = templates[0][0];
                }
                console.log(templates);
            })
    }
    It should work fine.
    If outside, it could cause synchronization issues. You need to use async/await to resolve this.
  • ALAN365 Profile Picture
    on at

    Thank you Steve.

    As per your suggestion, I added console.log(templates); in then() and run the code, here is debug result:

    docgen.JPG

    templates array is still empty. How to fix it? Thanks

  • ALAN365 Profile Picture
    on at

    Hi,

    Have an idea, please.

  • ALAN365 Profile Picture
    on at

    I have wrapped up the code in a HTML file to test, the code like below:

    <html>

    <head>

       <meta content="text/html; charset = ISO-8859-1" http-equiv="content-type">

       <script src="ajax.googleapis.com/.../script>

       <script type="application/javascript">

           function loadJSON() {

               debugger;

               var url = "prod-xx.canadaeast.logic.azure.com:xxxxxxxxxxxxxxxxxxxxxxxxxx;

               // var params = {

               //     "get_templates": "Yes"

               // }

               var templates = []; //global templates variable

               const requestOptions = {

                   method: 'POST',

                   headers: {

                       'Content-Type': 'application/json'

                   },

                   body: JSON.stringify({

                       get_templates: 'Yes'

                   })

               };

               fetch(url, requestOptions)

                   .then(response => response.json())

                   .then(data => {

                       alert(JSON.stringify(data));

                       for (var i in data)

                           templates.push([data[i]]);

                       templates = templates[0][0];

                       console.log(templates);

                   })

           }

       </script>

       <title>Document Generation</title>

    </head>

    <body>

       <h1>Template Info</h1>

       <div class="central">

           <button type="button" onclick="loadJSON()">Show Info </button>

       </div>

       <div id="templateName"></div>

       <div id="templateId"></div>

    </body>

    </html>

    Cannot get templates value. Please give me advice. Thanks.

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
Martin Dráb Profile Picture

Martin Dráb 47 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 38 Super User 2025 Season 2

#3
#ManoVerse Profile Picture

#ManoVerse 31

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans