EDiaz6
October 24, 2023, 12:47am
1
In a Workflow, I’m trying to get all the contacts with an email account as an example to see how it works, however, I’m getting this error:
TypeError: Cannot read properties of undefined (reading 'makeRequestContext')
here’s my code:
const hubspot = require('@hubspot/api-client');
exports.main = async (event, callback) => {
const hubspotClient = new hubspot.Client({
accessToken: process.env.WFInternalAPIToken
});
const email = event.inputFields['email'];
let phone;
try {
const ApiResponse = await hubspotClient.crm.contacts.searchApi.doSearch('v3', {
filterGroups: [
{
filters: [
{
propertyName: 'email',
operator: 'EQ',
value: email,
},
],
},
],
});
let contacts = ApiResponse.body.results;
phone=contacts[0].properties.phone;
} catch (err) {
console.error(err);
throw err;
}
callback({
outputFields: {
email: email,
phone: phone
}
});
}
I’m hoping to use it with custom objects once this works, so if someone can help me out to see whats going on I would be forever grateful!
You are missing below code line at the begining of your script
const hubspot = require('@hubspot/api-client');
and authorization
const hubspotClient = new hubspot.Client({
accessToken: process.env.SECRET_NAME
});
it should look like that
const hubspot = require('@hubspot/api-client');
exports.main = async (event, callback) => {
const hubspotClient = new hubspot.Client({
accessToken: process.env.SECRET_NAME
});
const email = event.inputFields['email'];
let phone;
try {
const ApiResponse = await hubspotClient.crm.contacts.searchApi.doSearch('v3', {
filterGroups: [
{
filters: [
{
propertyName: 'email',
operator: 'EQ',
value: email,
},
],
},
],
});
let contacts = ApiResponse.body.results;
phone=contacts[0].properties.phone;
} catch (err) {
console.error(err);
throw err;
}
}
Please refer to templates availavble here
If you are new to Custom Code step in Workflows and don’t know how to authorise please have a look on how to create Private Apps and how to use Tokens in Cusom Codes
https://community.hubspot.com/t5/9881-Operations-Hub/Custom-Code-Get-firstname-amp-lastname-from-email-address/m-p/859925/highlight/true#M1829
EDiaz6
October 24, 2023, 4:51pm
3
Hi! Thanks for your reply, I do have those lines, just updated my post to show the full code, I used as a base the one you mentioned and was able to successfully retrieve values using the getbyId method, but what I want is to succesfully use the search method (code above), that’s whats giving me the error.
My private app access token also has the custom objects scope
Hi,
I made it work but with use of Axios library. It’s more readable for me and easier to troubleshoot.
const axios = require('axios');
exports.main = async (event, callback) => {
const email = event.inputFields['email'];
let data = JSON.stringify({
"filterGroups": [{
"filters": [{
"propertyName": "email",
"operator": "EQ",
"value": email
}]
}]
});
let config = {
method: 'post',
maxBodyLength: Infinity,
url: 'https://api.hubapi.com/crm/v3/objects/contacts/search',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + process.env.SECRET_NAME
},
data: data
};
axios.request(config).then((response) => {
console.log(response.data.results[0].properties.phone)
}).catch((error) => {
console.log(error);
});
}
EDiaz6
October 25, 2023, 6:19pm
5
Awesome! whou rocks? you rock!! Thanks a lot!! now I can finally move on