Created
January 10, 2019 04:46
-
-
Save oeon/dd21e8744a0f705d396f35ae7e6f4960 to your computer and use it in GitHub Desktop.
Google Apps Script for creating Google Calendar events via Fulcrum Webhooks
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 = ""; | |
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"]; | |
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() { | |
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>"}); | |
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