Search code examples
javascriptpower-automatepower-automate-desktop

How to access flow variables from javascript in power automate


I'm working on a power automate flow and I'm having some trouble. I copied some text to my clipboard in the flow and i asigned it to a variable in my flow. I'm trying to parse the variable using the javascript scripting feature but i'm getting a syntax error whenever I try to use the %variable% syntax in javascript. I used the variable button in the text editor they give you to add the variable to my script but even something simple as var res = %SimResults%; returns an error C:\Users\$me\AppData\Local\Temp\Robin\nkwrgcdoxrp.tmp(1, 11) Microsoft JScript compilation error: Syntax error

It seems that even though I Should be able to access my flow variables from Javascript it throws a syntax error when I try


Solution

  • You'll need to put quotes around it ...

    var res = "%SimResults%"
    

    ... it treats the variables as more of a find and replace within your Javascript code, therefore, you need to do the work to ensure the correct encapsulation, etc. exists.

    This may not solve your problem though depending on the complexity of what is contained within the variable.

    If you have additional quotes, etc. you may need to escape them prior to putting them in the Javascript task IF they exist within the string ... just be mindful of that.