JavaScript code inline in Azure Logic Apps workflow

A Logic App workflow that triggers when a new Outlook email arrives, sends an approval email, executes JavaScript code, and performs additional actions.

Scenario

  1. Trigger the workflow when a new email arrives in Outlook.
  2. Send an approval email to a user.
  3. Wait for the user’s response (approve/reject).
  4. Execute JavaScript code based on the response.
  5. Send a confirmation email.

Step-by-Step Workflow

Step 1: Create a Logic App

  1. Go to the Azure portal.
  2. Click on Create a resource > Integration > Logic App.
  3. Provide a name, subscription, resource group, and location for your Logic App.
  4. Click Review + create and then Create.

Step 2: Add an Outlook Trigger

  1. Open your Logic App in the designer.
  2. Search for the Outlook connector and select When a new email arrives (V2).
  3. Sign in to your Outlook account to authorize the connection.
  4. Configure the trigger:
      • Folder: Inbox (or any specific folder)
      • Include Attachments: No (or Yes, if needed)
      • Importance: Any (or High, if needed)
      • Include Attachments: No (or Yes, if needed)
      • Only with Attachments: No (or Yes, if needed)

Step 3: Send an Approval Email

  1. Add a new action after the Outlook trigger.
  2. Search for Approvals and select Send approval email.
  3. Configure the approval email:
    • User Options: Approve/Reject
    • SubjectApproval Request
    • BodyDo you want to approve this email?
    • Assigned To: Enter the email address of the approver.

Step 4: Execute JavaScript Code

    1. Add a new action after the Send approval email action.
    2. Search for Inline Code and add it.
    3. If this is your first time using the Inline Code action, you will be prompted to create an Integration Account. Follow the prompts to create one.
    4. Write your JavaScript code snippet. For example:
      // Check if the approval response is "Approve"
      var response = workflowContext.actions.Send_approval_email.outputs.body.SelectedOption;
      var output = (response === "Approve") ? "Approved" : "Rejected";
      return output;

Step 5: Perform Additional Actions

    1. Add a new action after the Inline Code action.
    2. Use the output of the JavaScript snippet in subsequent actions. For example:
      • Send a confirmation email.
      • Save the result to a file.

Here's how Logic App should look:

estudy247-la-test-javascript-13

Example Workflow

  1. Outlook Trigger:
    • Trigger: When a new email arrives in Outlook.
  2. Send an Approval Email:
    • Subject: Approval Request
    • Body: Do you want to approve this email?
    • Assigned To: approver@example.com
  3. Inline Code Action:
    • Code:
      // Check if the approval response is "Approve"
      var response = workflowContext.actions.Send_approval_email.outputs.body.SelectedOption;
      var output = (response === "Approve") ? "Approved" : "Rejected";
      return output;
  4. Send Confirmation Email:
    • Tosender@example.com
    • SubjectApproval Result
    • Body:
      The email has been @{body('Execute_JavaScript_Code')}.

Testing the Workflow

  1. Save and run the Logic App.
  2. Send a test email to your Outlook account.
  3. The workflow will:
    • Trigger when the email arrives.
    • Send an approval email to the approver.
    • Execute JavaScript code to determine the result.
    • Send a confirmation email with the result.

Sample Output

The approval request email will look like this:
estudy247-la-test-javascript-approvalmail
The confirmation email will look like this:
estudy247-la-test-javascript-approvalresult

You can download the logic app template from the estudy247 GitHub repository – la-test-javascript-13

Post a comment

Leave a Comment

Scroll to Top