Select Section...

iAuditor users often want to perform an action for each failed response in an audit. For example, you may want an email to be sent for each failed response, which results in a new order being created in a work order management system.

To execute one task per response in an audit (assuming the response meets a certain condition) a workflow with two Zaps is needed. This page will show you how to set up this 2-zap workflow using Zapier.

We’ll assume we want to send an email for every failed response in an audit, for every audit conducted from a specific template.

The first step is to register for a free or paid Zapier account. For help on getting started with Zapier, please see our Zapier Integrations article.

Once you have a Zapier account, log in and select “Make A Zap!”. You’ll be taken to a blank Zap template to select a trigger app. Type in “iAuditor” in the search box.

 

 

 

 

 

 

 

 

 

 

We want to set this Zap to trigger only on a specific template, so we select “Audit Completion” as the trigger.  If we wanted this Zap to trigger any time any audit is conducted, we would select “Any Audit Completion” instead. Click “Save + Continue.”

Now associate your SafetyCulture account with Zapier by clicking “Connect a New Account.”

A separate window will pop up prompting you to enter in the API token. If you have already generated a token, input it in the text field and click “Yes, Continue”.

If you have not generated a token, click on the hyperlink “My Profile.” This will direct you to SafetyCulture, the web app, to generate the API Access Token.  In SafetyCulture, the web app, type your password into the input box and click “Generate Token”. Copy your token by highlighting it, then right-click and click copy.

api token

Input your API Access Token into Zapier and click “Yes, Continue.”

“Test” your account to ensure connectivity.

“Save + Continue” takes us to the “Set Up Options” step, where we select our target template. Select the template you want to work with and hit “Continue.” Provided you have recently conducted an audit from this template, “Fetch and Continue” will complete the trigger setup.

For this workflow, the only Action required is Code by Zapier

.

Select “Run Python”, then “Save + Continue.” To configure the input data, type ‘audit_id’ in the first Input Data field (exactly as shown, any deviation from this will cause an error), and select the Audit ID value from the dropdown on the right column, as shown.

We have provided a simple example script to help you get started here and encourage you to modify it for your own needs. Delete the code that is pre-filled in the “Code” section of the Zap, and copy and paste the following script in its place:

import requests
auth_header = {'Authorization': 'Bearer YOUR_SAFETYCULTURE_API_TOKEN'}
webhook_url = 'YOUR_WEBHOOK_URL'
api_url = 'https://api.safetyculture.io/audits/'
audit_id = input['audit_id']
audit_doc = requests.get(api_url + audit_id, headers=auth_header).json()
failed_items = []
audit_author = audit_doc['audit_data']['authorship']['author']
conducted_on = audit_doc['audit_data']['date_completed']
conducted_on = conducted_on[:conducted_on.index('T')]
audit_title = audit_doc['template_data']['metadata']['name']
for item in audit_doc['items']:
    if item.get('responses') and item['responses'].get('failed') == True:
        label = item.get('label')
        if label is None:
            label = 'no_label'
        responses = item['responses']
        response_label = responses['selected'][0]['label']
        notes = responses.get('text')
        if notes is None:
            notes = ''
        failed_items.append({'label': label,
            'response_label': response_label,
            'conducted_on': conducted_on,
            'notes': notes,
            'author': audit_author
        })
for item in failed_items:
    r = requests.post(webhook_url, data = item)


You’ll notice in the code there are two values which are not filled out. The first is the value YOUR_SAFETYCULTURE_API_TOKEN. Replace this with your actual token, which we obtained in a previous step (replace the entire term YOUR_SAFETYCULTURE_API_TOKEN, including the angle brackets). The other term is YOUR_WEBHOOK_URL. We’ll obtain this value in the next step.

In a second tab, open Zapier in your browser again. Again, select “Make a Zap”. This time, the trigger will be “Webhooks by Zapier.”

And the selected trigger will be Catch Hook.

Click “Save + Continue,” and then skip past the next stage, where Zapier asks if you’d like to “Pick off a child key.”  We do not want to.

Leave it blank, and hit “Continue”. Now, we finally obtain our webhook URL.

Copy this, and place it into the Code by Zapier script in the first Zap we created (i.e. the first Zap’s ‘Code by Zapier’ Action)

Now, on the first Zap’s Action, scroll down and select “Continue”, then “Create and Continue”. If you got no errors, you’re done with this Zap! Select “Finish”, name your Zap and toggle it to On.

Switch back to our second Zap (the Webhook Zap), and select “Okay, I did this”

Your test should be successful, as completing the first Zap should have sent a request to this webhook. Hit “Continue” to move on to the next step, configuring the action which will be conducted every time a failed item is sent to the webhook. For this tutorial, we’ll use “Email by Zapier.” You can substitute this with any action you like.

“Send Outbound Email” is the only option available for this action, so let’s select it and “Save + Continue” to configure the email. Select a valid “To” address and enter in a subject line. Then, select the “Add Item” icon in the “Body” section. Here, you’ll see every value we sent to the webhook from the Code by Zapier script earlier.

You can add any or all of these values, as well as free typing text

The rest of the fields are optional – fill them out if you choose to, and then select “Continue” to test. You’ll see a preview of what would be sent. Select “Create + Continue” to test it. The “To” address you selected should receive a test email. You can safely delete this as it was to test the connection. Select “Finish”, name your Zap and turn it on, and you’re done! It’s a good idea now to test this workflow end-to-end by conducting an audit from the template we set up in the first step, and selectively failing some items. The expected result is a single email per failed item.