Trying to make a Google Apps script so that everytime I press "Get Contact" on RocketReach (a Lead Generation app) and it displays the profile of the contact including email, it goes to the webhook I deployed via Google Apps Script and pastes the completed lookup into my Google Sheets.
Here is the code I used:
function doPost(e) {
try {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Company Leads'); // Change 'Sheet1' to your sheet name
// Log the entire event object
Logger.log("Received event: " + JSON.stringify(e));
// Check if postData is defined
if (!e || !e.postData) {
throw new Error("No postData available in the event object");
}
// Log the postData contents
Logger.log("postData contents: " + e.postData.contents);
var data = JSON.parse(e.postData.contents);
// Log the parsed data
Logger.log("Parsed data: " + JSON.stringify(data));
// Check if all required fields are present
if (!data.full_name || !data.current_employer || !data.occupation || !data.email) {
throw new Error("Incomplete data received. Data: " + JSON.stringify(data));
}
var fullName = data.full_name;
var nameParts = fullName.split(' ');
var firstName = nameParts[0];
var lastName = nameParts.slice(1).join(' '); // Join the rest of the parts as last name
var currentEmployer = data.current_employer;
var occupation = data.occupation;
var email = data.email;
sheet.appendRow([new Date(), currentEmployer, firstName, lastName, occupation, email]);
return ContentService.createTextOutput(JSON.stringify({result: 'success'})).setMimeType(ContentService.MimeType.JSON);
} catch (error) {
Logger.log("Error: " + error.message);
return ContentService.createTextOutput(JSON.stringify({result: 'error', message: error.message})).setMimeType(ContentService.MimeType.JSON);
}
}
However it's not displaying any contacts to my sheet. It keeps giving me the log "Received event: undefined" and "Error: No postData available in the event object".
What am I missing?
doublecheck your configuration setup. you can perform manual test like:
function testDoPost() {
var testData = {
postData: {
contents: JSON.stringify({
full_name: "John Doe",
current_employer: "Example Corp",
occupation: "Software Engineer",
email: "[email protected]"
})
}
};
var result = doPost(testData);
Logger.log("Test result: " + result.getContent());
}