# Debugging Custom Code

**URL:** <https://community.hubspot.com/t/debugging-custom-code/53019>\
**Category:** Data Hub\
**Created:** [March 11, 2022, 9:31pm UTC](https://community.hubspot.com/t/debugging-custom-code/53019 "2022-03-11T21:31:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KBoyd0](https://avatars.discourse-cdn.com/v4/letter/k/dfb087/32.png) [@KBoyd0](https://community.hubspot.com/u/KBoyd0)\
**Post date:** [March 11, 2022, 9:31pm UTC](https://community.hubspot.com/t/debugging-custom-code/53019/1 "2022-03-11T21:31:15Z")

</div>

Hi all, I’m using the following custom code to create a referral program within Hubspot (I’m actually following instructions I found online from a fellow Hubspoter). I keep getting an error message - it seems like I’m not defining the output variable correctly. Any ideas on what I’m doing wrong here?

 ![code.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/9/2/92abaecd13a8ecd81d2fd747435dec8b61e3bb18.png)

 ![error code.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/7/d/7dff02da5116cd3b86edb581ff7dde866637b739.png)

 ![error message.png](https://d2qpr2jzcazej7.cloudfront.net/original/1X/f85ecc1d48c4354e5f2f9cebac907db2eecc0e77.png)

---

<div class="post-metadata">

**Author:** ![ChrisoKlepke](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/chrisoklepke/32/11213_2.png) [@ChrisoKlepke](https://community.hubspot.com/u/ChrisoKlepke)\
**Post date:** [March 14, 2022, 11:20am UTC](https://community.hubspot.com/t/debugging-custom-code/53019/2 "2022-03-14T11:20:10Z")

</div>

Hey @KBoyd0 ,

just to be sure. Did you add the Data Output field here:

 ![Screen Shot 2022-03-14 at 12.18.15.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/f/7/f72308afbbde423bc8ba7f338430e8441b2b2e55.png)

Cheers,

Chriso

---

<div class="post-metadata">

**Author:** ![nikodev](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/nikodev/32/44733_2.png) [@nikodev](https://community.hubspot.com/u/nikodev)\
**Post date:** [April 14, 2022, 10:47pm UTC](https://community.hubspot.com/t/debugging-custom-code/53019/3 "2022-04-14T22:47:26Z")

</div>

Hey there @KBoyd0. I think you’re running into a scope related issue. I’d recommend something similar to the below, where you’re setting the referrer\_id as the return value of your API request chain, so the “res” variable ends up equating to the referral code you’re generating. Let me know if you have any questions. I’ve tested this for you successfully.  
Let me know if you have any questions!

```lia-code-sample
const hubspot = require("@hubspot/api-client");

exports.main = async (event, callback) => {
	const hubspotClient = new hubspot.Client({
		apiKey: process.env.API_KEY
	});

	try {
		const res = await hubspotClient.crm.contacts.basicApi
			.getById(event.object.objectId)
			.then((response) => {
				let newTest = Math.floor(Math.random() * 1000) + Date.now();
				return newTest;
			});

		callback({
			outputFields: {
				test: res
			}
		});
	} catch (err) {
		console.error(err);
		// We will automatically retry when the code fails because of a rate limiting error from the HubSpot API.
		throw err;
	}
}; 

```

 ![](https://d2qpr2jzcazej7.cloudfront.net/original/3X/e/8/e8fde6114cef8d77c7ebe3a9c1bcec3965869e19.png)
