Hello Sparktans,
In this article I will be showing you how to get the Spark Webhook to POST to your Google Sheets. First thing you're going to need is this Script(This is our Example Template some adjustments will be needed) to add to your Google Sheets.
function doPost(e) {
var spreadsheetId = 'Insert-Google-Sheet-ID'; // Your Spreadsheet ID
var sheet = SpreadsheetApp.openById(spreadsheetId).getSheets()[0]; // Access the first sheet by index
// Check if the sheet was retrieved successfully
if (!sheet) {
Logger.log("Error: Sheet not found.");
return ContentService.createTextOutput(JSON.stringify({ result: 'failure', error: 'Sheet not found' }));
}
try {
// Log the entire event object to see what is being received
Logger.log('POST request received: ' + JSON.stringify(e));
// Safely extract the parameters from the POST request
var params = e && e.parameter ? e.parameter : {};
// Extract the specific parameters
var firstname = params.firstname;
var lastname = params.lastname;
var email = params.email;
var locationID = params.locationID;
var contactID = params.contactID;
// Log each parameter for debugging purposes
Logger.log("Firstname: " + firstname);
Logger.log("Lastname: " + lastname);
Logger.log("Email: " + email);
Logger.log("LocationID: " + locationID);
Logger.log("ContactID: " + contactID);
// Check if all required fields are present
if (firstname && lastname && email && locationID && contactID) {
// Append data to the Google Sheet
sheet.appendRow([firstname, lastname, email, locationID, contactID]);
Logger.log('Row appended successfully');
} else {
Logger.log('Missing fields in POST request');
return ContentService.createTextOutput(JSON.stringify({ result: 'failure', error: 'Missing fields in data' }));
}
} catch (error) {
Logger.log('Error during POST request: ' + error.message); // Log any errors for debugging
return ContentService.createTextOutput(JSON.stringify({ result: 'failure', error: error.message }));
}
return ContentService.createTextOutput(JSON.stringify({ result: 'success' }));
}
This code is just an example and while it would work for you there are many other Merge fields you can use that if you are sending you must add onto this code for those merge fields. Keep in mind if you are adding to this code you need to have some knowledge of what you're doing otherwise you could run in to error if adding onto it incorrectly and not know how to fix the issue. You can see all the Merge Fields you can use for a webhook in this Article:
https://help.sparkmembership.com/en/articles/5973829-post-to-webhook-automation-action
Once you have this Script All you would have to do is go to Google Sheets, Select the Google sheet or Create a new one.
Select Extensions - App Sheet
Insert the Script:
Make sure you insert the Google Sheet ID in to the Script at the very top where it says Insert-Google-Sheet-ID. You can find you google sheet ID from the URL like so
Once you have that placed in there you would Deploy-New Deployment
For this to work you would make sure the Type is Web App
After that you would just have to insert the description. The Main thing is to make sure 'Execute' is set to ME and Anyone to 'who has access'. Select Deploy after that.
Once you hit Deploy you would be able to grab the Web App URL.
This is the URL you would use to add in to your Spark Action step for webhook.
The Example used in the Post Variables is the example I used as well to make the template for this Google Sheet Script so if you copy and paste that in to the Variable field this script would work as intended without making changes.
If you run in to any issues please reach out to out Tech Support team and we would be able to assist you or connect you to a team member that can. Reminder this should be handled by Developers or someone with knowledge of Javascript/Google Apps Script.