Skip to content

Instantly share code, notes, and snippets.

@jackcoldrick90
Last active September 12, 2023 20:27
Show Gist options
  • Save jackcoldrick90/b38268080650f8531650c2b47f681d8d to your computer and use it in GitHub Desktop.
Save jackcoldrick90/b38268080650f8531650c2b47f681d8d to your computer and use it in GitHub Desktop.
This script looks at the "referrer" property associated with the contact that has just converted and then leverages the CRM Search APi to find the matching contact with that specific "referrer ID". It then uses the CRM API to update the contacts "recent referral date" and "total referral" properties.
const hubspot = require('@hubspot/api-client');
exports.main = (event, callback) => {
const hubspotClient = new hubspot.Client({
apiKey: process.env.HAPIKEY
});
//1) Get the enrolled deal - referrer_id property value
hubspotClient.crm.contacts.basicApi.getById(event.object.objectId, ["referrer"])
.then(results => {
let referrer = results.body.properties.referrer; //contact Referrer ID
console.log(referrer);
//2) Search for the contact using the deals referrer_id and update
const filter = { propertyName: 'referrer_id', operator: 'EQ', value: referrer }
const filterGroup = { filters: [filter] }
const sort = JSON.stringify({ propertyName: 'referrer_id', direction: 'DESCENDING' })
const properties = ['referrer_id', 'total_referrals', 'firstname', 'lastname', 'email']
const limit = 100
const after = 0
const publicObjectSearchRequest = {
filterGroups: [filterGroup],
sorts: [sort],
properties,
limit,
after,
}
hubspotClient.crm.contacts.searchApi.doSearch(publicObjectSearchRequest).then(results =>{
let contactId = results.body.results[0].id;
let referred_by = results.body.results[0].properties.email
let totalReferrals = 0;
if(results.body.results[0].properties.total_referrals === null || results.body.results[0].properties.total_referrals === undefined || results.body.results[0].properties.total_referrals === ""){
totalReferrals = 0;
}else{
totalReferrals = parseInt(results.body.results[0].properties.total_referrals)
}
let totalReferralsUpdated = totalReferrals + 1;
var d = new Date();
d.setUTCHours(0,0,0,0);
hubspotClient.crm.contacts.basicApi.update(contactId, {"properties":{"total_referrals": totalReferralsUpdated, "recent_referral_date": d}});
callback({
outputFields: {
reffered_by: referred_by
}
});
});
});
}
const hubspot = require('@hubspot/api-client');
exports.main = (event, callback) => {
const hubspotClient = new hubspot.Client({
apiKey: process.env.HAPIKEY
});
const referrer = event.inputFields['referrer'];
console.log("REFERRED ID: " + referrer);
//2) Search for the contact using the deals referrer_id and update
const filter = {
propertyName: 'referrer_id',
operator: 'EQ',
value: referrer
}
const filterGroup = {
filters: [filter]
}
const sort = JSON.stringify({
propertyName: 'referrer_id',
direction: 'DESCENDING'
})
const properties = ['referrer_id', 'total_referrals', 'firstname', 'lastname', 'email']
const limit = 100
const after = 0
const publicObjectSearchRequest = {
filterGroups: [filterGroup],
sorts: [sort],
properties,
limit,
after,
}
hubspotClient.crm.contacts.searchApi.doSearch(publicObjectSearchRequest).then(results => {
let contactId = results.body.results[0].id;
let referrer_email = results.body.results[0].properties.email
let totalReferrals = 0;
if (results.body.results[0].properties.total_referrals === null || results.body.results[0].properties.total_referrals === undefined || results.body.results[0].properties.total_referrals === "") {
totalReferrals = 0;
} else {
totalReferrals = parseInt(results.body.results[0].properties.total_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": referrer_email
}
})
//Update Referring Contact
hubspotClient.crm.contacts.basicApi.update(contactId, {
"properties": {
"total_referrals": totalReferralsUpdated,
"recent_referral_date": d
}
})
});
}
const hubspot = require('@hubspot/api-client');
exports.main = (event, callback) => {
const hubspotClient = new hubspot.Client({
apiKey: process.env.HAPIKEY
});
//1) Get the enrolled deal - referrer_id property value
hubspotClient.crm.contacts.basicApi.getById(event.object.objectId, ["referrer"])
.then(results => {
let referrer = results.body.properties.referrer; //contact Referrer ID
//2) Search for the contact using the deals referrer_id and update
const filter = { propertyName: 'referrer_id', operator: 'EQ', value: referrer }
const filterGroup = { filters: [filter] }
const sort = JSON.stringify({ propertyName: 'referrer_id', direction: 'DESCENDING' })
const properties = ['referrer_id', 'total_referrals', 'firstname', 'lastname']
const limit = 100
const after = 0
const publicObjectSearchRequest = {
filterGroups: [filterGroup],
sorts: [sort],
properties,
limit,
after,
}
hubspotClient.crm.contacts.searchApi.doSearch(publicObjectSearchRequest).then(results =>{
let contactId = results.body.results[0].id;
let totalReferrals = 0;
if(results.body.results[0].properties.total_referrals === null || results.body.results[0].properties.total_referrals === undefined || results.body.results[0].properties.total_referrals === ""){
totalReferrals = 0;
}else{
totalReferrals = parseInt(results.body.results[0].properties.total_referrals)
}
let totalReferralsUpdated = totalReferrals + 1;
var d = new Date();
d.setUTCHours(0,0,0,0);
hubspotClient.crm.contacts.basicApi.update(contactId, {"properties":{"total_referrals": totalReferralsUpdated, "recent_referral_date": d}});
});
});
}
@zengoma
Copy link

zengoma commented Sep 12, 2023

@jyejamesdisisto, your error stack reveals that you're getting an API authentication error. Please note that you need to name your secret HAPIKEY and not PrivateApp_AccessToken in this action (it differs from the first action in that way)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment