Debugging Custom Code

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?

Hey @KBoyd0 ,

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

Cheers,

Chriso

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!

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;
	}
};