Created
February 6, 2019 00:01
-
-
Save oeon/53b8a5123cb7a26f1c7f0b3b9f7e1229 to your computer and use it in GitHub Desktop.
Google Apps Script for creating Google Calendar events via Fulcrum Webhooks with Location
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Google Apps Script for creating Google Calendar events via Fulcrum Webhooks | |
// replace your Fulcrum API token here | |
var token = ""; | |
// leave createEventId as an empty string. We'll get it in createEvent() | |
var createEventId = ""; | |
function doPost(e) { | |
return handleResponse(e); | |
} | |
function handleResponse(e) { | |
var jsonString = e.postData.getDataAsString(); | |
var payload = JSON.parse(jsonString); | |
// replace your form id here | |
var form = "46e2df6b-6e51-4008-ab47-a140ef3694f3"; | |
var record_id = payload.data.id; | |
// replace your field keys below | |
var title = payload.data.form_values["7f36"]; | |
var start_unix = parseInt(payload.data.form_values["88bd"]); | |
var stop_unix = parseInt(payload.data.form_values["1755"]); | |
var iCalId = payload.data.form_values["d922"]; | |
// for capturing an Event Location, add an Address field and a hidden Calculation field using FORMATADDRESS https://developer.fulcrumapp.com/expressions/reference/formataddress/ below is the Calculation field | |
var streetAddress = payload.data.form_values["80fd"]; | |
var startTime = new Date(start_unix); | |
var stopTime = new Date(stop_unix); | |
if (payload.data.form_id === form) { | |
if (payload.type === "record.create") { | |
createEvent(); | |
updateRecord(record_id, createEventId); | |
} else if (payload.type === "record.update") { | |
updateEvent(); | |
} else if (payload.type === "record.delete") { | |
deleteEvent(); | |
} | |
} | |
function createEvent() { | |
// https://developers.google.com/apps-script/reference/calendar/calendar#createEvent(String,Date,Date,Object) | |
var event = CalendarApp.getDefaultCalendar().createEvent(title, startTime, stopTime, {description: "<a href='https://web.fulcrumapp.com/records/" + record_id + "'>Desktop Link</a><br><a href='https://www.fulcrumapp.com/action/#edit-record?form_id=" + form + "&record_id=" + record_id + "'>Mobile Link</a>", location: streetAddress}); | |
createEventId = event.getId(); | |
} | |
function updateEvent() { | |
CalendarApp.getEventById(iCalId).setTime(startTime, stopTime).setTitle(title); | |
} | |
function deleteEvent() { | |
CalendarApp.getEventById(iCalId).deleteEvent(); | |
} | |
// this function will write the Google Calendar EventID back to the Fulcrum record for updating/deleting | |
function updateRecord(record, event) { | |
var url = "https://api.fulcrumapp.com/api/v2/records/" + record + ".json"; | |
var data = UrlFetchApp.fetch(url, { | |
"method": "GET", | |
"headers": { | |
"X-ApiToken": token, | |
"Accept": "application/json" | |
} | |
}); | |
var json = JSON.parse(data); | |
// replace your field key here | |
json.record.form_values['d922'] = event; | |
delete json.record.changeset_id; | |
var update = UrlFetchApp.fetch("https://api.fulcrumapp.com/api/v2/records.json", { | |
"method": "POST", | |
"contentType": "application/json", | |
"payload": JSON.stringify(json), | |
"headers": { | |
"X-ApiToken": token, | |
"Accept": "application/json" | |
} | |
}); | |
Logger.log(update); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment