Execute code action
Unlock advanced automation capabilities within your workflows by running custom JavaScript or TypeScript in the Execute Code action. This feature lets you implement complex logic, transform data dynamically, connect with third-party systems, and use embedded AI Assist to generate and refine code from natural-language prompts inside the editor.
Calling third-party APIs? Don’t put API keys, tokens, or passwords in your code. Save each credential in Settings > Organization > Security > Credential store with its allowed hosts, then reference it by name using aw.secret and aw.secretHeader. Atomicwork fills in the real value at send time — it never shows up in code, run history, or logs. See Managing credentials for the full guide.
Understand the power of custom scripts
By incorporating custom code into your workflows, you gain granular control over your automation processes. You can:
-
Implement complex business logic: Execute sophisticated conditional logic that goes beyond standard workflow conditions.
-
Transform data on the fly: Modify inputs, reformat data, or prepare data structures for subsequent actions or external systems. You can leverage dynamic values from requests using data pills and placeholders.
-
Integrate with external services: Make API calls to third-party systems, fetch data, or trigger actions in other platforms directly from your script.
-
Use connected app functions: Call
Workflow.*orAPI.*functions for integrated apps such as Jira, Slack, Okta, and Azure AD. This is how you use Call API — it’s available exclusively from code scripts and handles authentication, token refresh, and destination safety automatically.
For instance, imagine you’re automating new employee onboarding and need to create user accounts in Okta. A custom script can automatically check if a proposed username already exists. If it does, the script can intelligently modify the username (e.g., changing “andrea.s@company.com” to “andrea.s1@company.com”) before proceeding with account creation. This proactive approach helps prevent errors and significantly reduces manual intervention.
Set up a workflow to use the Execute Code action
To add custom JavaScript or TypeScript to a workflow, follow these steps:
-
Go to Automations > Workflow library. Select a workspace for request, incident and change workflows, or select Organization for asset, user and directory object workflows. You can find more details on general workflow creation in our article on setting up workflows.
-
Click the Create a workflow button or select an existing workflow to edit.
-
Enter a name and description for your workflow if creating a new one.
-
Configure a trigger for your workflow, such as Request is created. Triggers determine when your workflow will run.
-
Click Add an action (or the + icon) to add a new step to your workflow.
-
From the list of available actions, select Execute code under Core Utilities.
-
In the action configuration panel, go to the Code tab.
-
Use the Input Data tab to define key-value inputs with static values or placeholders from the trigger and earlier actions. These values are available in your script through the
inputobject, and you can switch to the Code tab to write or generate your JavaScript or TypeScript. -
Enter or paste your custom JavaScript or TypeScript into the editor. You can also use
Workflow.*functions from connected integrations such as Jira, Slack, Okta, and Azure AD. TypeWorkflow., press Cmd+I or Ctrl+I, or click Insert function to browse the built-in function library. -
Once you have added your script, click Done to save the action.
Generate code with AI Assist
In the code editor, click the AI sparkle icon or Generate with AI to open AI Assist. Describe the code you want in natural language to the coding agent, and continue the conversation to refine the result until it matches your workflow.
AI Assist inserts the last generated code block into the editor, so you can review it, test it, and continue editing it manually if needed.
Using AI Assist with credentials: Tell AI Assist which saved credentials to use by name — never paste a key into the chat. For example: “Get a Microsoft Graph app token using my saved credentials azure-client-id and azure-client-secret, then read the organization’s display name.” Before you publish, check that the generated code uses aw.secret or aw.secretHeader and has no literal keys. See Use saved credentials in code for more.
-
Subsequent actions in workflows can consume and use the JSON output data from this action.
-
After configuring all triggers, conditions, and actions, click Publish at the top right corner to activate your workflow.
Use the full-screen editor
Select the expand icon to open the full-screen editor. The left panel shows the function library, the center panel contains the Input Data and Code tabs, and the right side opens a toggleable AI Assist panel for AI-generated code.
Test code before publishing
Use Setup test data or Run test to open the test panel for the action. Add sample values for your inputs, then review the output, console logs, and any errors in the test results. After a successful test run, the returned output can be used as placeholders in later workflow actions.
Key Considerations:
-
Data Access: Your scripts can interact with workflow data using data pills and placeholders to dynamically pull request details or outputs from previous actions.
-
External Calls: Use saved credentials for any third-party API call — see the callout above.
-
Return Value: Your code must return a JSON object so its output can be used as placeholders in downstream workflow actions.
-
Error Handling: Implement robust error handling within your JavaScript or TypeScript to manage unexpected situations and prevent workflow failures. Failed code actions stop the workflow unless you open the action’s Settings tab and choose Continue workflow.
Executing custom JavaScript or TypeScript gives you flexibility to tailor workflows to your exact needs. You can write code manually, generate it with AI Assist, test it with sample data, and use the output to power later workflow steps.
For more information on related workflow capabilities, explore these articles:
Troubleshoot common issues
-
Invalid return value: Return a JSON object, not a string, number, array, or undefined value.
-
Empty response: Confirm that your code includes a
returnstatement that returns a non-null object. -
Output too large: Reduce the amount of data your code returns and keep only the values later actions need.
-
Runtime failure: Check for syntax errors, unhandled exceptions, or long-running code.
-
AI Assist panel errors: If the AI panel shows an error, verify network connectivity and try again.
-
Too many active agents: Wait for the current AI generation to finish before starting another one.
-
Test and production differences: Test runs use the current admin’s credentials, while workflow runs use a workspace admin’s credentials. Confirm that integrations and placeholders are configured for the runtime environment.
-
Credential errors: If you see
"Credentials not allowed on this URL"or"Webhook execution failed.", check that the URL’s host is listed in the credential’s allowed hosts at Settings > Organization > Security > Credential store. If the credential shows.*, edit it to replace.*with the actual hostnames. See Managing credentials for details.
Learn more
-
Use saved credentials in code —
aw.secret,aw.secretHeader, code recipes, and error reference -
Managing credentials — setting up credentials and allowed hosts
For broader workflow generation and editing with natural-language prompts, see Build Workflows with AI Assist.
