Hi all,
It seems you cannot call
const hubspot = require(‘@hubspot/api-client’);
in a “run code snippet” action in a chatflow conversation.
So then how would one call an HubSpot API Endpoint from inside a chatflow?
I am trying to write some code which searches through a specific objectType to return results.
// Import the Hubspot NodeJS Client Library - this will allow us to use the HubSpot APIs
const hubspot = require(‘@hubspot/api-client’);
/*
This function is called when the custom code action is executed.
*/
exports.main = (event, callback) => {
const objectType = “2-25573472”; // This is the object type ID for the “Location” object in HubSpot
let searchResults = [];
//const inviteeEmail = event.inputFields[‘email’];
const city = event.userMessage.message
const hubspotClient = new hubspot.Client({ accessToken: “tokenHERE” });
// Create search criteria
const filter = { propertyName: ‘city’, operator: ‘EQ’, value: city }
const filterGroup = { filters: [filter] }
const sort = JSON.stringify({ propertyName: ‘city’, direction: ‘DESCENDING’})
const properties = [‘name’]
const searchCriteria = {
filterGroups: [filterGroup],
sorts: [sort],
properties
}
// Search the CRM for Location matching “city” variable defined earlier
hubspotClient.crm.objects.searchApi.doSearch(objectType, searchCriteria).then(searchLocationResponse => {
// If total equals 0 no results found
if(searchLocationResponse.total != 0){ //NO MATCH FOUND
searchResults = searchLocationResponse.results;
// If a match is found, return the Location object
const responseJson = {
botMessage: `I found ${searchResults.length} locations matching the city ${city}.`,
responseExpected: false,
searchResults: searchResults
}
callback(responseJson);
} else {
// If no match is found, return an error message
const responseJson = {
botMessage: `I’m sorry, I couldn’t find any locations matching the city ${city}.`,
responseExpected: false
}
callback(responseJson);
}
});
}