Fulcrum was built to be extensible, enabling users to integrate best-in-class data collection tools with other applications and services for complete workflow customization. We provide a variety of integration points for getting data both into and out of Fulcrum and offer a full range of Professional Services if you need help getting things wired up.
Drive has all the features one would expect of a modern file storage platform, including a generous free storage quota, secure sharing mechanisms, mobile apps for both Android & iOS, and desktop apps for integrated backup and sync. This, combined with Apps Script’s ability to set timed triggers and make external API requests makes it ideal for automating the import and export of Fulcrum data.
In our first scenario, we will configure a system where CSV files can be saved to a folder on Drive and a script running on a timer will periodically check the folder for new files. If any new files have been added since the last run, the script will parse the CSV file, loop through the rows and import each row as a new record into a defined Fulcrum app.
We will also set it up so that after all the records have been imported, the CSV file will be moved from the Files to Import folder to the Imported Files folder and an email will be sent to notify an admin of the number of records imported. This workflow allows us to monitor the system and recover any CSV files for further inspection or troubleshooting. The timed trigger also has configurable notifications which will email the script owner if it fails to execute.
Note that Apps Script does come with some daily quotas and hard limitations we need to be aware of, specifically the 100MB / day total URL Fetch data received, 10MB / call URL Fetch POST size, and 6 min / execution script runtime limits. You may need a more robust solution for frequently importing large files with tens of thousands of records, but for seeding tens or hundreds of records, this should work well.
First up, we want to create the folders to hold the files in our Drive account. Assuming you have a Google account and are logged in, navigate your browser to https://drive.google.com and create a root folder to store everything. Give the folder a name and click Create. I named mine Fulcrum Imports.
Once the folder has been created, double click on it to enter the folder and follow the same process to create the 2 folders for storing the CSV files (Files to Import and Imported Files). If someone else is going to be uploading the files, you can give them access by right clicking the folder and selecting Share from the context menu. You may also want to install the Backup and Sync app on a computer so the CSV files can be programmatically moved or written to the shared drive rather than manually uploaded via the web.
Now that the folders have been configured, we’re ready to start writing the logic to actually import the records. If your Drive is already connected to App Scripts, you should be able to create a new file within the root folder you created above.
You may have to click the Connect more apps button and search for “apps script” to connect it first. You can also create the file at https://script.google.com/home and just save it to your folder. It doesn’t matter where this file is created but keeping everything organized together is always a good idea.
Creating a new file will open a blank Apps Script code editor where you can begin to write your code. First rename the file from Untitled project to something more meaningful like Fulcrum Importer by clicking on the name in the top left corner. Now you can copy and paste the code from this gist into the code window.
The code should be fairly self-explanatory with notes to help explain what’s going on, but here’s how it works:
Once you’ve updated the code with your particular details, save the Apps Script file and give it a test by placing a CSV file with a couple of test rows in the import folder and manually triggering the script by clicking Run > Run function > getFiles. The first time you run this you will be prompted to authorize the script to view and manage the files in your Google Drive, send email as you, and connect to an external service via your Google account.
If everything runs successfully, you should receive an email at the address you provided and the rows in the CSV file should have been imported as records in your Fulcrum app. If you run into any errors, it’s likely an issue with the record JSON. You may want to try hardcoding this and running it again to help with debugging. Apps Script also has a Logger class for writing out text to the debugging logs.
Once you’ve verified that the script is working as expected, you can set up a trigger so it will run automatically without any user input. Click Edit > Current project's triggers and click the link to add a new trigger. You want to run the getFiles function with a Time-driven event and specify the timer type and time range. I’ve set the trigger to run the script daily between midnight and 1am so new records should be available for field crews in the morning.
Now that we’ve walked through the basic process of wiring up Drive with Fulcrum for imports, we can easily expand upon this technique to schedule a daily data export of records from Fulcrum. The only addition we need for this is an exportData function to fetch the data via Fulcrum’s Query API and write it to a Drive folder.
Follow the same process above to create a project folder (Fulcrum Exports) with a folder inside it to store the files (Files). Note the ID of the Files folder for the script to write to below.
Create a new App Script file in the project folder (Fulcrum Exporter) and copy and paste the code from this gist into the code window.
In this example we are writing a SQL query to fetch all the columns for all the records WHERE _server_updated_at >= NOW() - '1 day'::INTERVAL. This is using the _server_updated_at timestamp to filter out only the records that have been updated within the last 24 hours, which can be combined with a daily trigger to automatically run our daily export.
This has been a fairly technical post walking through the process of integrating Fulcrum with Google Drive for automating data imports and exports via the Fulcrum API. Hopefully it has provided some useful concepts and code samples for you to begin extending or automating your own data collection workflow, but our Professional Services team is always available to help you get the most out of your Fulcrum experience! We’d love to hear how Fulcrum fits into your broader workflow, so feel free to share your tips and tricks @fulcrumapp.