Search code examples
javascriptjscriptpower-automate-desktop

How to return result from JavaScript action in Power Automate Desktop?


If I use a flow action Run JavaScript function on web page in Power Automate Desktop, I am able to use following code:

function ExecuteScript() { 
  alert('foo');              // shown as pop up messag 
  console.log('baa');  // goes to browser dev tools console
  return 'result'         // stored as output variable of the flow action
}

and see the return value as value of the output variable

enter image description here

However, if I use the same code for a flow action Run JavaScript, the value of the output variable is not set.

=> How to return a result from the the "Run JavaScript" flow action?


Solution

  • The Run JavaScript action uses a different language then the Run JavaScript function on web page. It uses JScript.NET, an ECMAscript based dialect from Microsoft. (In my opinion the action should be called "Run JScript" to avoid confusion.).

    a) In order to return a value from the action use WScript.echo

    WScript.echo("result")
    

    b) The result variable might contain an additional CRLF that needs to be considered when processing the result in further actions.

    c) If you want to be able to show/handle errors, you need to enable the %ScriptError% variable, which is disabled by default. Also see related question

    How to show/handle errors thrown while running JavaScript flow actions in Power Automate Desktop?

    Sources:

    https://powerusers.microsoft.com/t5/Power-Automate-Desktop/Executing-JavaScript/td-p/865149

    https://learn.microsoft.com/en-us/power-automate/desktop-flows/actions-reference/scripting#working-with-variables-in-scripting-actions

    Related:

    How can I write a simple JScript input/output program?

    https://learn.microsoft.com/en-us/previous-versions/ms950396(v=msdn.10)?redirectedfrom=MSDN

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference