JotForm/Asana Integration

At work, one of our clients used a program called vCreative for production orders. Since they were our biggest client at the time we used the software for our production orders as well. vCreative was built for radio and voice production orders. As a digital agency this software was a nightmare to use. We had to come up with all kind of inventive ways to make assignments and tasks.

The biggest issue was orders would come in without all the details needed because there was just a text area for notes and it wasn’t even required to submit an order. We needed something that could require specific questions before you could submit an order.

We selected JotForm for this exact purpose, but needed something to manage work orders and tasks as well. We settled on Asana as our task manager. Unfortunately JotForm’s integration with Asans was well below what we needed.

I created a WordPress hook and CRON job to host on our company site that utilized both JotForm’s and Asana’s APIs. My plugin checks JotForm every 2 minutes for the past 10 submissions and checks Asana to see if that task has already been added. If not, it parses through the JotForm submission and pulls out specific field data in order to add the task to Asana in the right project, with notes, assignments, due dates, etc.

Pull in orders and run checks

<?php
require_once('jotform/JotForm.php');

// Setup
$jotformAPI = new JotForm("API CODE HERE");
$date = strtotime(date('Y-m-d') . ' -1 month');
$olddate = date("Y-m-d", $date);
//Get Submissions
$submissions = $jotformAPI->getFormSubmissions('FORM ID HERE', 0, 10, NULL, 'created_at' ); 


//Get orders in Asana
$ch = curl_init('https://app.asana.com/api/1.0/workspaces/WORKSPACE ID HERE/tasks/search?projects.any=PROJECT ID HERE&created_on.after=' . $olddate . '&opt_fields=custom_fields&sort_by=created_at&opt_pretty');
$header = array(
    'Accept: application/json',
    'Content-type:text/json;charset=utf-8',
    'Authorization: Bearer 0/API KEY HERE');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$curl = curl_exec($ch);

$asana = json_decode($curl, JSON_PRETTY_PRINT)['data'];
$asanaTask = [];

foreach ($asana as $task ){ 
    foreach ($task['custom_fields'] as $customField) { 
        if($customField['gid'] === "GID HERE") {
            $asanaTask[] = $customField['text_value'];
        }
    }
} 
//endforeach 
curl_close($ch);

If all the checks pass, submit them to Asana.

foreach($submissions as $submission) {
    $id = $submission['id'];

    if(in_array($id, $asanaTask)) {
        echo $id . ' is already created';
        continue;
    } //endif

    // Parse submission for data needed
    $company = $submission['answers'][5]['answer'];
    $name = $submission['answers'][31]['answer'];
    $url = 'https://www.jotform.com/submission/' . $id;
    $info = htmlspecialchars(str_replace(array("\n", "\r"),'\n',$submission['answers'][19]['answer']));
    $notes = "" . $url . '\n\n' . $info . "";
    $duedate = $submission['answers'][14]['answer']['year'] . '-' . $submission['answers'][14]['answer']['month'] . '-' . $submission['answers'][14]['answer']['day'];
    $assignee = $submission['answers'][11]['answer'];
    $followers = $submission['answers'][49]['answer'];
   
    if( array_key_exists('answer', $submission['answers'][29] )) {
        $followers = $followers . ', ' . $submission['answers'][29]['answer'];
    }
    $stationList = [];
    if(array_key_exists('answer', $submission['answers'][48]) && is_array($submission['answers'][48]['answer'])):
        foreach($submission['answers'][48]['answer'] as $station) {
            $stationList[] = '"' . $stationsArray[$station] . '"';
        }
        $stations = implode(", ", $stationList);
    elseif(array_key_exists('answer', $submission['answers'][47]) && ($submission['answers'][47]['answer'] != '')):
        $stations = '"' . $stationsArray[$submission['answers'][47]['answer']] . '"';
    elseif($company === 'Broadway Outdoor'):
        $stations = '"1152300233989822"';
    else:
        $stations = '"1107875390458336"';
    endif;

    $fields = '{
        "data": {
            "assignee": "' . $assignee . '",
            "html_notes": "' . $notes . '",
            "notes": "' . $notes . '",
            "followers": "' . $followers . '",
            "name": "' . $name . '",
            "projects": [
                "1145417634281240", "850113058002663", ' . $stations . '
            ],
            "custom_fields": {
                "1145417634281244": "' . $id . '"
            },
            "due_on": "' . $duedate . '"
        }
    }';

    $ch = curl_init('https://app.asana.com/api/1.0/tasks/');
    $header = array(
        'Accept: application/json',
        'Content-type:text/json;charset=utf-8',
        'Authorization: Bearer 0/API KEY HERE');
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

    $curl = curl_exec($ch); 
    $json = json_decode($curl, JSON_PRETTY_PRINT); 
    curl_close($ch);
} 
//endforeach $submissions
?>