Today, we held the third workshop in this month’s Masterclass in Custom Automation and we learned how to create a referral program!
Thank you all for showing up such great numbers! We hope you enjoyed it.
In this post, you’ll find all the resources for yesterday’s event including the recording, Q&A, the slide deck, GitHub repo, and the links to additional websites mentioned.
This was priceless, shout out to @jackcoldrick for putting this together
In a world where customer retention is the new customer acquisition strategy, having a referral program is critical for success and I look forward to customizing this for educators, consultants and trainers
@MTrusttheProc have you triple checked that the properties’ interal value is exactly right (ie capitalization)? That was my initial error.
Also, you should be using a private token and not HAPI key - but I dont think you should see any successes if you’re using a HAPI key. Our code is using:
Hi @callie Thanks for you feedback, below is my code, could you please share your code, I’m still having an error. Thank you in advance
/*
Operations Hub Workshop 3: Workflow 2 Code
This code is used to attribute a referral back to the correct contact. This is done by querying the CRM to find a contact matching
the referrer value associated to the enrolled contact. We then update the appropriate properties on both the referring and referred contact.
*/
//1. Import required libraries
const hubspot = require('@hubspot/api-client'); // HubSpot Node Client will allow us to make calls to HubSpot API
exports.main = (event, callback) => {
//2. Create our client
const hubspotClient = new hubspot.Client({
accessToken: process.env.APIKEY // Reference our API key - this should have been added as a secret
});
//3. Store the referrer ID linked to the enrolled contact in a variable
const referrer = event.inputFields['Referrer'];
//4. Configure our search query
const filterGroup = {
filters: [{ // filters are used to limit the results returned. We will simply filter for any contact who has a matching value for the referrer ID
propertyName: 'referrer_id',
operator: 'EQ',
value: referrer
}]
}
// We specify what properties we'd like to return from this search
const properties = ['referrer_id', 'number_of_referrals', 'firstname', 'lastname', 'email']
// We create our search object which we will pass to our api call
const publicObjectSearchRequest = {
filterGroups: [filterGroup],
properties
}
//5. Search the CRM for the contact using the query defined above
hubspotClient.crm.contacts.searchApi.doSearch(publicObjectSearchRequest).then(results => {
//6. Store data in variables
let contactId = results.body.results[0].id; // Id of the referring contact
let referred_by = results.body.results[0].properties.firstname + " " + results.body.results[0].properties.lastname; // full name of the referring contact
let totalReferrals = 0;
console.log("referred_by" + referred_by);
//7. Check to see if this is their first referral. If it is we set "totalReferrals" property to "0"
if (results.body.results[0].properties.number_of_referrals === null || results.body.results[0].properties.number_of_referrals === undefined || results.body.results[0].properties.number_of_referrals === "") {
totalReferrals = 0;
} else { // Otherwise we retrieve the current number of referrals
totalReferrals = parseInt(results.body.results[0].properties.number_of_referrals)
}
let totalReferralsUpdated = totalReferrals + 1; // Increment referrals
//8. Create a new date (todays date)
var d = new Date();
d.setUTCHours(0, 0, 0, 0);
//9. Update Referred Contact's "referred by" property
hubspotClient.crm.contacts.basicApi.update(event.object.objectId, {
"properties": {
"referred_by": referred_by
}
})
//10. Update Referring Contacts "number of referrals" and "recent referral date" properties
hubspotClient.crm.contacts.basicApi.update(contactId, {
"properties": {
"number_of_referrals": totalReferralsUpdated,
"recent_referral_date": d
}
})
});
}
/*
Operations Hub Workshop 3: Workflow 2 Code
This code is used to attribute a referral back to the correct contact. This is done by querying the CRM to find a contact matching
the referrer value associated to the enrolled contact. We then update the appropriate properties on both the referring and referred contact.
*/
//1. Import required libraries
const hubspot = require('@hubspot/api-client'); // HubSpot Node Client will allow us to make calls to HubSpot API
exports.main = (event, callback) => {
//2. Create our client
const hubspotClient = new hubspot.Client({
accessToken: process.env.HAPI_PrivateApp
});
//3. Store the referrer ID linked to the enrolled contact in a variable
const referrer = event.inputFields['referrer'];
//4. Configure our search query
const filterGroup = {
filters: [{ // filters are used to limit the results returned. We will simply filter for any contact who has a matching value for the referrer ID
propertyName: 'referrer_id',
operator: 'EQ',
value: referrer
}]
}
// We specify what properties we'd like to return from this search
const properties = ['referrer_id', 'number_of_referrals', 'firstname', 'lastname', 'email']
// We create our search object which we will pass to our api call
const publicObjectSearchRequest = {
filterGroups: [filterGroup],
properties
}
//5. Search the CRM for the contact using the query defined above
hubspotClient.crm.contacts.searchApi.doSearch(publicObjectSearchRequest).then(results => {
//6. Store data in variables
let contactId = results.body.results[0].id; // Id of the referring contact
let referred_by = results.body.results[0].properties.firstname + " " + results.body.results[0].properties.lastname; // full name of the referring contact
let totalReferrals = 0;
console.log("referred_by" + referred_by);
//7. Check to see if this is their first referral. If it is we set "totalReferrals" property to "0"
if (results.body.results[0].properties.number_of_referrals === null || results.body.results[0].properties.number_of_referrals === undefined || results.body.results[0].properties.number_of_referrals === "") {
totalReferrals = 0;
} else { // Otherwise we retrieve the current number of referrals
totalReferrals = parseInt(results.body.results[0].properties.number_of_referrals)
}
let totalReferralsUpdated = totalReferrals + 1; // Increment referrals
//8. Create a new date (todays date)
var d = new Date();
d.setUTCHours(0, 0, 0, 0);
//9. Update Referred Contact's "referred by" property
hubspotClient.crm.contacts.basicApi.update(event.object.objectId, {
"properties": {
"referred_by": referred_by
}
})
//10. Update Referring Contacts "number of referrals" and "recent referral date" properties
hubspotClient.crm.contacts.basicApi.update(contactId, {
"properties": {
"number_of_referrals": totalReferralsUpdated,
"recent_referral_date": d
}
})
});
}