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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Run JavaScript functio...
Power Automate
Answered

Run JavaScript function on web page returns data as well as function code

(0) ShareShare
ReportReport
Posted on by 51

When I run the following code to capture ld+json content, it returns the ld+json content as well as JS code at the end of the JSResult variable. This prevents the Convert JSON to customer object action from running. Any ideas why that code is being returned and how to prevent it?

 

Thanks, John

 

Run JavaScript function on web page JavaScript Function: 

function ExecuteScript() {
var headings = document.evaluate("//script[contains(., 'gtin')]", document, null, XPathResult.ANY_TYPE, null );
var thisHeading = headings.iterateNext();
var result = "";
while (thisHeading) {
result += thisHeading.innerText;
thisHeading = headings.iterateNext();
}
return result;
}

 

Weird code at end of JSResult variable.

var result = undefined; try{ result = (function ExecuteScript() {
var headings = document.evaluate("//script[contains(., 'gtin')]", document, null, XPathResult.ANY_TYPE, null );
var thisHeading = headings.iterateNext();
var result = "";
while (thisHeading) {
result += thisHeading.innerText;
thisHeading = headings.iterateNext();
}
return result;
})();;}catch(e){}; document.documentElement.setAttribute("result", result); document.documentElement.setAttribute("complete", "true");

I have the same question (0)
  • Verified answer
    jfk86d Profile Picture
    51 on at

    It appears that PAD may have a bug with its JavaScript action. The above code appears to run an extra loop in the while and for some reason echos the action's code. To prevent this I modified the loop as follows. This prevents the echo of the action's code to be included in the results.

     

    John

     

    function ExecuteScript() {
    var txtJSON= "[";
    var headings = document.evaluate("//script[contains(., 'gtin')]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
    for(var i = 0; i < headings.snapshotLength-1; i++) {
    txtJSON += headings.snapshotItem(i).innerText;
    if (i < headings.snapshotLength-2) {
    txtJSON += ", ";
    }
    }
    txtJSON += "]";
    return txtJSON;
    }

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 238 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 225

#3
Haque Profile Picture

Haque 181

Last 30 days Overall leaderboard