Hi team,
I'm working on a Refer a Friend workflow using a custom code action. The goal is to:
Look up the referrer contact based on a referrer field on the newly referred contact.
Increment the referrer’s number_of_referrals.
Set the referred contact's referred_by field.
However, while the search runs and logs show correct values, the update calls are not saving data to HubSpot.
Here’s the custom code I’m using inside the workflow:
const hubspot = require('@hubspot/api-client');
exports.main = (event, callback) => {
const hubspotClient = new hubspot.Client({
accessToken: process.env.HAPIKEY
});
const referrer = event.inputFields['referrer'];
const filterGroup = {
filters: [{
propertyName: 'referrer_id',
operator: 'EQ',
value: referrer
}]
};
const properties = ['referrer_id', 'number_of_referrals', 'firstname', 'lastname', 'email'];
const publicObjectSearchRequest = {
filterGroups: [filterGroup],
properties
};
hubspotClient.crm.contacts.searchApi.doSearch(publicObjectSearchRequest).then(results => {
let contactId = results.body.results[0].id;
let referred_by = results.body.results[0].properties.firstname + " " + results.body.results[0].properties.lastname;
let totalReferrals = 0;
if (!results.body.results[0].properties.number_of_referrals) {
totalReferrals = 0;
} else {
totalReferrals = parseInt(results.body.results[0].properties.number_of_referrals);
}
let totalReferralsUpdated = totalReferrals + 1;
var d = new Date();
d.setUTCHours(0, 0, 0, 0);
// Update referred contact
hubspotClient.crm.contacts.basicApi.update(event.object.objectId, {
properties: {
referred_by: referred_by
}
});
// Update referrer contact
hubspotClient.crm.contacts.basicApi.update(contactId, {
properties: {
number_of_referrals: totalReferralsUpdated,
recent_referral_date: d
}
});
});
};
Am I missing an await or .catch() for error handling? Or is there anything wrong with the way updates are triggered?
Appreciate any help!