isOpen: true, pages: 1

Execute JavaScript in Power Automate

You can run JavaScript code directly within a CustomJS action in your Microsoft Power Automate flow. If you want to manage and write complex JavaScript functions.

JavaScript Execution in Power Automate
JavaScript Execution in Power Automate

How it works

The customJS action consists of four fields: Connection, Input, JavaScript code and Return Type.


FieldDescriptionHow to use it
ConnectionLinks the action to the CustomJS service, enabling JavaScript execution.Select or create a connection (e.g., Coding Service). Usually set up once and reused across flows.
InputData passed into the JavaScript code, available as the input object.Enter plain text, arrays, or valid JSON strings. For Power Automate arrays, you may need to convert them to JSON first.
JavaScript CodeThe script that will be executed by CustomJS. Must follow JavaScript syntax.Ensure the code ends with a return statement so data is passed back to Power Automate.

Input variables

Inputs are accessed through the input object. For complex scenarios, you can:

  • Pass JSON objects as input (must be valid JSON strings).
  • Convert Power Automate arrays to JSON format if needed.
Inputs passed to your script are accessible as an input object. If you're passing multiple variables, it’s recommended to structure them as a JSON object for clarity and ease of use.
Inline JavaScript in Power Automate
Inline JavaScript in Power Automate

Await async results before returning

When using asynchronous code (like fetch, database calls, or API requests), await the result and then return it. If you forget await or don’t return the resolved value, Power Automate will just get a pending Promise, not usable data.


Without awaitWith await
Code
// Incorrect: returns a Promise, not data
return fetch(input.url).then(res => res.json());
// Correct: resolve the Promise before returning
const response = await fetch(input.url);
const data = await response.json();

return { statusCode: response.status, data: data };

ResultPower Automate receives a Promise instead of usable data. The next action cannot work with unresolved Promises.Power Automate receives the actual data (JSON object with status and content). The next action can directly use this result.

Remove inline comments

Inline comments usually work if you place them correctly with line breaks. However, misplaced comments can prevent the code from running as expected. Consider removing them if you have issues.


Inline comments Without inline comments
With inline comments
// return full name
return input.firstName + " " + input.lastName;
return input.firstName + " " + input.lastName;
Result Power Automate may not receive the intended output if the comment interrupts code execution. Comments cannot interrupt code execution.