# \[WRAP UP\] Custom Automation Masterclass - Workshop 3: Creating a Referral Program

**URL:** <https://community.hubspot.com/t/wrap-up-custom-automation-masterclass-workshop-3-creating-a-referral-program/51770>\
**Category:** 📝 Resources\
**Tags:** on-demand\
**Created:** [February 17, 2022, 5:18pm UTC](https://community.hubspot.com/t/wrap-up-custom-automation-masterclass-workshop-3-creating-a-referral-program/51770 "2022-02-17T17:18:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbogaert](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jbogaert/32/33027_2.png) [@jbogaert](https://community.hubspot.com/u/jbogaert)\
**Post date:** [February 17, 2022, 5:18pm UTC](https://community.hubspot.com/t/wrap-up-custom-automation-masterclass-workshop-3-creating-a-referral-program/51770/1 "2022-02-17T17:18:18Z")

</div>

![Community Resources.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/6/9/697191e5f4dd457fcbd333e6b17a884451aff62d.png)

Hi everyone!

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.

### **RECORDING**

[![](https://d2qpr2jzcazej7.cloudfront.net/original/3X/d/9/d996e5b357c868df3415bb622bab9ee72f6f922a.jpeg "Custom Automation Masterclass - Workshop 3: Creating a referral program") ](https://www.youtube.com/watch?v=a9DUa9WKdHs)

### [**Slide Deck**](https://docs.google.com/presentation/d/17XQcvFHefgddqTwmnfSQkAFQ7KDKTAaSir95OczkkWw/edit#slide=id.g100fd110b0e_0_122)

### [**GITHUB REPO**](https://gist.github.com/jackcoldrick90/3c23477fa93f2cde85bba6f3142f3edd)

### [**Q&A**](https://community.hubspot.com/t5/9881-Operations-Hub/Q-amp-A-Follow-up-Custom-Automation-Workshop-on-creating-a/td-p/578794)

### **Additional Links**

- [https://developers.hubspot.com/](https://developers.hubspot.com/)
- Operations Hub Community: [https://community.hubspot.com/t5/9881-Operations-Hub/bd-p/Operations\_Hub](https://community.hubspot.com/t5/9881-Operations-Hub/bd-p/Operations_Hub)
- Programmable Automation Use Case Library: [Data Hub Use Case Library](https://www.hubspot.com/programmable-automation-use-cases)

### **Don’t forget to sign up for next session:**

### [Custom Automation Workshop 4: Querying an external database](https://events.hubspot.com/events/details/hubspot-hubspot-academy-presents-custom-automation-masterclass-workshop-4-querying-an-external-database/)

---

<div class="post-metadata">

**Author:** ![MuseDebbie](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/musedebbie/32/14370_2.png) [@MuseDebbie](https://community.hubspot.com/u/MuseDebbie)\
**Post date:** [February 22, 2022, 8:46am UTC](https://community.hubspot.com/t/wrap-up-custom-automation-masterclass-workshop-3-creating-a-referral-program/51770/2 "2022-02-22T08:46:16Z")

</div>

Thanks for sharing this @jbogaert.😎

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 😎

GrOw Team GrOw 🚀

---

<div class="post-metadata">

**Author:** ![callie](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/callie/32/93038_2.png) [@callie](https://community.hubspot.com/u/callie)\
**Post date:** [May 2, 2022, 7:54pm UTC](https://community.hubspot.com/t/wrap-up-custom-automation-masterclass-workshop-3-creating-a-referral-program/51770/3 "2022-05-02T19:54:52Z")

</div>

Loved this live and am getting more from it now watching it back!

---

<div class="post-metadata">

**Author:** ![MTrusttheProc](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@MTrusttheProc](https://community.hubspot.com/u/MTrusttheProc)\
**Post date:** [November 1, 2023, 4:55am UTC](https://community.hubspot.com/t/wrap-up-custom-automation-masterclass-workshop-3-creating-a-referral-program/51770/4 "2023-11-01T04:55:22Z")

</div>

Thanks for sharing this @jbogaert.  
@jbogaert @callie @MuseDebbie  
Just curious how to solve the error from this code  
[https://gist.githubusercontent.com/jackcoldrick90/3c23477fa93f2cde85bba6f3142f3edd/raw/9133250496af582a2925b0df70fcc7042704fbea/operations\_hub\_workshop\_3\_workflow\_2\_code.js](https://gist.githubusercontent.com/jackcoldrick90/3c23477fa93f2cde85bba6f3142f3edd/raw/9133250496af582a2925b0df70fcc7042704fbea/operations_hub_workshop_3_workflow_2_code.js)  
The error is WARNING: The logs for this function have exceeded the 4KB limit.  
Also in Contacts, only Refferer and Referrer ID is updating, the rest is not working [Number of Referrals, Recent Referral Date and Referred By]. Please let me know if anyone can help, the code is exactly the same in the video, it might be outdated etc… Thanking you in advance

---

<div class="post-metadata">

**Author:** ![callie](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/callie/32/93038_2.png) [@callie](https://community.hubspot.com/u/callie)\
**Post date:** [November 2, 2023, 8:04pm UTC](https://community.hubspot.com/t/wrap-up-custom-automation-masterclass-workshop-3-creating-a-referral-program/51770/5 "2023-11-02T20:04:55Z")

</div>

@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:

`accessToken: process.env.HAPI_PrivateApp​`

---

<div class="post-metadata">

**Author:** ![MTrusttheProc](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@MTrusttheProc](https://community.hubspot.com/u/MTrusttheProc)\
**Post date:** [November 3, 2023, 1:47am UTC](https://community.hubspot.com/t/wrap-up-custom-automation-masterclass-workshop-3-creating-a-referral-program/51770/6 "2023-11-03T01:47:13Z")

</div>

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

```lia-code-sample
/*
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
 }
 })
 });
}

```

---

<div class="post-metadata">

**Author:** ![callie](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/callie/32/93038_2.png) [@callie](https://community.hubspot.com/u/callie)\
**Post date:** [November 3, 2023, 3:14pm UTC](https://community.hubspot.com/t/wrap-up-custom-automation-masterclass-workshop-3-creating-a-referral-program/51770/7 "2023-11-03T15:14:10Z")

</div>

```lia-code-sample
/*
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
 }
 })
 });
}

```
