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" actio...
Power Automate
Answered

"Run JavaScript" action, using existing variable.

(0) ShareShare
ReportReport
Posted on by 60

In working through what I thought would be a simple flow, came across a need to strip HTML from some text. I settled on using a "Run JavaScript" action to pass the value through in order to return a plaintext string of text. Unfortunately, I'm finding that as soon as I try to incorporate any variable from within the PAD environment into the JavaScript, the whole action (Run JavaScript) silently fails.

 

To test this, I set a variable of EventDescription to the value of:

 

 

<p>Caffè Lena and the Saratoga Springs Public Library join together to celebrate Banned Books Week. On October 5, enjoy music from Reese Fulmer and Dan Berggren, as well as local authors who will share readings from titles that have been banned or challenged in the past 60 years.</p>

<p>Both organizations recognize the contributions of authors and illustrators amid the current national uptick in efforts to ban controversial books from libraries, suppressing conversations and the ability to think for ourselves.</p>

<p>This program is presented by</p>

 

 

 

My "Run JavaScript" action contains the JavaScript code below:

 

 

var myVar = "%EventDescription%";
WScript.Echo(myVar);

 

 

 

After running just these two actions, the return value of the "Run JavaScript" action, set to default of JavascriptOutput, is empty. If I change "myVar" to a value that is not rendered from a PAD environment variable, but just insert text inside the quotes, JavascriptOutput has a value.

 

There aren't many examples online of how to properly take advantage of this action, and how to properly inject external variables into "Scripting" actions. I've also tried assigning the myVar variable to %EventDescription% without surrounding quotes, without success.

 

Any thoughts as to what I need to do to fix this? I have to imagine I'm doing something quite silly.

 

When %EventDescription% is wrapped in quotes, I get a ScriptError of "Unterminated string constant". When not wrapped in quotes, I get a ScriptError of "Syntax error".

I have the same question (0)
  • Verified answer
    Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    First of all, you could just use Replace text with regular expressions enabled to replace <[^>]*> (which represents any characters between < and > that are not >) with %""% (which represents an empty string. You don't really need JS to do this.

     

    The second thing here is the "Unterminated string constant" error. This article covers in detail: https://bobbyhadz.com/blog/javascript-unterminated-string-constant#:~:text=The%20%22Unterminated%20string%20constant%22%20error,split%20across%20multiple%20lines%20incorrectly.

     

    The thing is that your string spans over multiple lines. This is not supported by JS, unless you escape the newlines or replace them. In vanilla JS, you could use a backtick character ` to enclose the string in instead of quotes. That would then make it work with multi-line strings. However, I tried that and it seems like it doesn't work with PAD for some reason. It throws an Invalid character exception.

     

    So, your best bet here is to simply use Replace text as suggested previously. Try setting it up like this:

    Agnius_0-1696651820820.png

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

  • BrendonKoz Profile Picture
    60 on at

    Thank you, @Agnius! This is my 2nd attempt at using PAD and this particular flow is completely different than the first, so I hadn't done any major text work; I'm not yet familiar with all of the available actions - I didn't realize Replace text offered REGEX. Thank you for the tip, it works perfectly, and thanks too for clarifying why I was getting a JavaScript error due to newlines. I (obviously) didn't even think about newlines.

     

    I only made one small adjustment to your REGEX which was to make the glob character a plus character, and then make it not greedy; I didn't want it grabbing more than necessary. I don't know if PAD would be terribly greedy, but it felt better as a safety precaution.

     

    <[^>]+?>

     

    I very much appreciate your help on that!! ...and feel a little silly for not catching it.

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    The question mark is great, but in general when you do a [^>]+>, it will essentially include everything except for >, until it reaches the first >. So, it shouldn't be possible to have it "greedy" here. But in general it is a good practice to do what you did there.

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 Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Vish WR Profile Picture

Vish WR 796

#2
Valantis Profile Picture

Valantis 568

#3
Haque Profile Picture

Haque 538

Last 30 days Overall leaderboard