# Need to understand custom code

**URL:** <https://community.hubspot.com/t/need-to-understand-custom-code/80201>\
**Category:** APIs & Integrations\
**Created:** [April 27, 2023, 4:59pm UTC](https://community.hubspot.com/t/need-to-understand-custom-code/80201 "2023-04-27T16:59:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DSaini1](https://avatars.discourse-cdn.com/v4/letter/d/df788c/32.png) [@DSaini1](https://community.hubspot.com/u/DSaini1)\
**Post date:** [April 27, 2023, 4:59pm UTC](https://community.hubspot.com/t/need-to-understand-custom-code/80201/1 "2023-04-27T16:59:17Z")

</div>

Hi,

I’m new to Hubspot custom code option in workflow. I have some code in workflow custom code. Please help me what is this code related to.

//1. Import required libraries  
const hubspot = require(‘@hubspot/api-client’); // HubSpot Node Client will allow us to make calls to HubSpot API  
exports.main = async (event, callback) =\> {  
const hubspotClient = new hubspot.Client({“accessToken”:“pat-na1-XXXXXXXXXXXXXXXX”});

//2. Get email address of Contact (Prospect in Salesforce) Owner assigned by SF in a variable  
var sfOwnerEmail = event.inputFields[‘sf\_owner\_email’];  
const sfOwnerName = event.inputFields[‘prospect\_owner\_\_sf\_’];  
console.log(“Salesforce Owner Email ::::::” + sfOwnerEmail);  
console.log(“Salesforce Owner Name ::::::” + sfOwnerName);  
if(sfOwnerName == “abc”){  
sfOwnerEmail = “[test@abc.com](mailto:test@abc.com)”;  
}  
else if(sfOwnerName == “xyz”){  
sfOwnerEmail = “[test1@abc.com](mailto:test1@abc.com)”;  
}

try {

//5. Sending the request to HS Owner API to get desired HS User details.  
const response = await hubspotClient.apiRequest({  
method: ‘get’,  
path: ‘/owners/v2/owners’,  
qs: {email: sfOwnerEmail}  
})  
console.log(“Response JSON String ::::::” + JSON.stringify(response.body, null, 2));  
var i;  
loop1:  
for(i=0; i \< response.body.length; i++){  
for(var propertyKey in response.body[i]) {  
// console.log(“key:”+propertyKey+“, propertyValue:”+response.body[i][propertyKey]);  
if(propertyKey == “type” && response.body[i][propertyKey] == “PERSON”){  
break loop1;  
}  
}  
}  
//6.. Extracting the first PERSON OwnerId property from response JSON  
var ownerId = response.body[i].ownerId;  
var ownerEmail = response.body[i].email;  
console.log(“OwnerId PropertyValue::::::” + ownerId);  
console.log(“Email Property::::::” + ownerEmail);

//7. Updating the Contact that triggered the workflow, with OwnerId to assign owner.  
const apiResponse = await hubspotClient.crm.contacts.basicApi.update(event.object.objectId, {  
“properties”: {  
“hubspot\_owner\_id”: ownerId  
}  
})  
}  
catch (e) {  
e.message === ‘HTTP request failed’ ? console.error(JSON.stringify(e.response, null, 2)) : console.error(e)  
}  
}

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [April 28, 2023, 4:51pm UTC](https://community.hubspot.com/t/need-to-understand-custom-code/80201/2 "2023-04-28T16:51:20Z")

</div>

Hi, @DSaini1 👋 Thanks for your post. Out of curiosity, is this code that you inherited?

Here’s a high-level view of what this code is set up to do:

- The code starts by importing the required libraries to make API calls to HubSpot from Node.js
- The script retrieves the email address of a Salesforce contact owner and sets it to a variable. It also retrieves the Salesforce name of the contact owner and sets it to another variable
- The code checks if the Salesforce contact owner name is equal to either “abc” or “xyz”, and if it is, it sets the email address in the previous variable to a specific test email address
- The script sends an API request to HubSpot to retrieve information about the user associated with the email address in the variable. Specifically, it retrieves the owner ID property and email property of the user
- The code updates the contact record for the contact that triggered the workflow with the retrieved owner ID property as the value for the “hubspot\_owner\_id” property of the contact record.

I hope this helps get you moving forward.

Have fun building! — Jaycee
