> ## Documentation Index
> Fetch the complete documentation index at: https://docs.firstresonance.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Automatically Send Purchases to Suppliers

> Automatically send emails to the supplier with the purchase PDF attached.

<Note>
  This page was auto-migrated from the legacy ION manual. Some images and cross-links may need follow-up cleanup. Original: `_legacy-export/automations/automatically-send-purchases-to-suppliers.md`.
</Note>

## Overview

[https://www.loom.com/share/2835812179514a45a45960c1b0094755](https://www.loom.com/share/2835812179514a45a45960c1b0094755)

## Setup

1. In ION, navigate to integrations, and go to the marketplace. Activate the "Auto-send Purchase Emails to Suppliers" integration. Go to the configuration.

<figure>
  <img src="https://mintcdn.com/firstresonance/YrwX258m7a4En2z1/images/legacy/2023-04-19%2011.29.01.gif?s=495eb19d1ff85443bc06fdf81f180677" alt="" width="1916" height="942" data-path="images/legacy/2023-04-19 11.29.01.gif" />

  <figcaption />
</figure>

2. Let's first configure the Microsoft Outlook Connection. This will require creating a new "App Registration" in your Azure Active Directory. See the Microsoft Outlook OAuth instructions [here](https://prismatic.io/docs/components/ms-outlook/#oauth-20-authorization-code) for further details. Also, check out the video below. Once you have a new app created in Active Directory, you can input the client secret value into the ION integration (make sure to not share secret values with anyone!). Finally, click connect in the integration configuration, which will trigger one final round of authentication.

[https://www.loom.com/share/a8c5981eb3324444aa548a37391efbde](https://www.loom.com/share/a8c5981eb3324444aa548a37391efbde)

3. Add an ION API key. You can find information on how to create this [here](../api/api-keys.md). Input the `clientId` and `clientSecret` into the integration configuration after you have generated the API key.
4. Ensure that the configuration settings are good (they will have a green icon next to them) and close the modal. Start with **test mode** on so that you can make sure the email is properly sent to you.
5. We now need to instruct ION on when to trigger the integration. In the organization settings of ION, create a custom attribute for purchase orders named "Send email to supplier". Make sure this is a header purchase attribute and not a line level attribute!

   <figure>
     <img src="https://mintcdn.com/firstresonance/rku--fZNcQwFktZc/images/legacy/image%20(226).png?fit=max&auto=format&n=rku--fZNcQwFktZc&q=85&s=b56ce3437428ca3e5eccf3622ff52c65" alt="" width="970" height="138" data-path="images/legacy/image (226).png" />

     <figcaption />
   </figure>
6. Add a new webhook to ION to listen for changes to this attribute. More info on webhooks [here](https://manual.firstresonance.io/api/webhooks).
   * Add a new webhook receiver using the integration's webhook URL. The URL is available under the "Test" tab in the integration. Use the code below in the `graphiQL` editor to generate the receiver. Make sure to replace the `<webhook URL here>` with that URL.

<figure>
  <img src="https://mintcdn.com/firstresonance/UUGTBonXVpXYwvEo/images/legacy/image%20(151).png?fit=max&auto=format&n=UUGTBonXVpXYwvEo&q=85&s=a282ceeb78d7dad01c5edcf86e5eeb08" alt="" width="1911" height="486" data-path="images/legacy/image (151).png" />

  <figcaption />
</figure>

```
mutation CreateWebhookReceiver($input: CreateWebhookReceiverInput!) {
    createWebhookReceiver(input: $input) {
        webhookReceiver {
            id name description webhookUri sharedSecret contentType expectedResponseCode
            active subscriptions { resource action id } headers { key value id }
        }
    }
}

{
  "input": {
    "name": "Purchase email sender",
    "webhookUri": "<webhook URL here>"
  }
}
```

* Now we need to instruct ION when to signal the receiver. Call the below mutation to listen for purchase order attribute changes. Make sure to update the input below with `receiverId` from the previous step.

```
mutation CreateWebhookSubscription($input: CreateWebhookSubscriptionInput!) {
    createWebhookSubscription(input: $input) {
        webhookSubscription {
            resource action id active receiverId events { id status }
        }
    }
}

{
  "input": {
    "receiverId": <ReceiverId from previous step>,
    "resource": "PURCHASE_ORDERS_ATTRIBUTES",
    "action": "UPDATE"
  }
}
```

* This will listen for anytime the attribute changes, but we now we need to also listen for when the attribute is first populated. Add one more subscription as shown below to listen for the creation of the attribute:

```
mutation CreateWebhookSubscription($input: CreateWebhookSubscriptionInput!) {
    createWebhookSubscription(input: $input) {
        webhookSubscription {
            resource action id active receiverId events { id status }
        }
    }
}

{
  "input": {
    "receiverId": <ReceiverId from previous step>,
    "resource": "PURCHASE_ORDERS_ATTRIBUTES",
    "action": "CREATE"
  }
}
```

7. That's it! Now you should be ready to automatically trigger purchase emails with attached PDFs. Turn **test mode** off once you confirm the email is working. When test mode is off, it will send the email to your suppliers based on the email from the supplier information in ION.
