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 / Fail to read an array ...
Power Automate
Unanswered

Fail to read an array returned from javascript function on a web page in dekstop power automate

(0) ShareShare
ReportReport
Posted on by 9

I have the following javascript function in my flow (desktop power automate):

 

 

 

function ExecuteScript() { 
 	var aTags = document.getElementsByClassName("gridviewlist");
 	var tdTags = aTags[0].getElementsByTagName("td");
 	var searchText = "%CurrentFood%";
 	
 	for (var i = 0; i < tdTags.length; i++) {
 		 if (tdTags[i].textContent.toLowerCase() == searchText.toLowerCase()) {
 			 nutrientName = tdTags[i+3].textContent;
 			 nutrientUnit = tdTags[i+4].textContent;
 		 }
 	}
 	
 	const nutrientNU = [nutrientName, nutrientUnit];
 	return nutrientNU;
}

 

 

I want to return an array which has 2 items. However, when I look at in the Flow Variables, it shows the following:

 

boomcheesebuger_0-1677209206386.png

 

How do I return the 2 items from javascript function so that I can get them in the flow?

Thanks,
Andy

  • Pstork1 Profile Picture
    69,661 Most Valuable Professional on at

    Your javascript function returns an object with two properties, not an array.  You should be able to access the value of the two properties directly from your object.  It should be something like 

    %NutrientNU[NutrientName]%
    %NutrientNU[nutrientUnit]%
  • boomcheesebuger Profile Picture
    9 on at

    I have tried your answer, but I have the following error:

     

    boomcheesebuger_0-1677210914819.png

     

  • Pstork1 Profile Picture
    69,661 Most Valuable Professional on at

    Sorry, I forgot to put the single quotes around the property names.

    %NutrientNU['NutrientName']%
    %NutrientNU['nutrientUnit']%

    That should work if the javascript is returning the object with named properties.  If the values are being returned as an object and property names aren't included I'm not sure how you reference the values.  But it will be some variation on the code above.

  • boomcheesebuger Profile Picture
    9 on at

    Following your hint, I have changed the javascript function as follows:

     

    boomcheesebuger_0-1677296361646.png

     

    I still got the following error:

    boomcheesebuger_1-1677296421370.png

     

  • Pstork1 Profile Picture
    69,661 Most Valuable Professional on at

    Try adding the single quotes around the property names in the Javascript also.

  • waynewalls Profile Picture
    74 on at

    The documentation for PAD indicates that the result returned by the Run JavaScript Function action is a text value.  The "[object Object]" returned from your function is likely the result of the default toString() method called on nutrientNU.  

     

    I also encounter "[object Object]" when I try and return a text value that is null or undefined.

     

    Try returning a string from your function.  Something like:

     

    return nutrientNew + ", " + nutrientUnit;

     

    Then use PAD's text actions to parse it once you have it back inside the flow.

  • AndyStewart Profile Picture
    18 on at

    Try using JSON.stringify(variable) to convert it to a string and return that...

  • waynewalls Profile Picture
    74 on at

    Hi @AndyStewart 

     

    Using JSON.stringify() was my first inclination too.  Except when I found out it also returned the string "[object Object]".  At that point I was under a deadline and just had to make something work.

     

    Subsequently, based on the MDN documentation I found that JSON.stringify() does not always return a string.  When something is undefined it returns undefined.  Which, I infer, is serialized somewhere in the PAD/extension processing to the string "[object Object]".

     

    I now use something like

    if (variable_to_be_returned === undefined) return ("undefined")
    else return (JSON.stringify(variable_to_be_returned)

     

    As an aside, the MDN docs indicate that there are other instances when JSON.stringify() does not return a string or accurately reflect the true result (e.g., NaN is returned as "null").  These do not apply to my current situation but might provide useful debugging information for others.

     

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 July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 227 Super User 2026 Season 2

#2
11manish Profile Picture

11manish 221 Super User 2026 Season 2

#3
Mohsin Ali Profile Picture

Mohsin Ali 132

Last 30 days Overall leaderboard