<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Need to understand custom code in APIs &amp; Integrations</title>
    <link>https://community.hubspot.com/t5/APIs-Integrations/Need-to-understand-custom-code/m-p/787350#M63559</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;//1. Import required libraries&lt;BR /&gt;const hubspot = require('@hubspot/api-client'); // HubSpot Node Client will allow us to make calls to HubSpot API&lt;BR /&gt;&lt;BR /&gt;exports.main = async (event, callback) =&amp;gt; {&lt;BR /&gt;&lt;BR /&gt;const hubspotClient = new hubspot.Client({"accessToken":"pat-na1-XXXXXXXXXXXXXXXX"});&lt;/P&gt;&lt;P&gt;//2. Get email address of Contact (Prospect in Salesforce) Owner assigned by SF in a variable&lt;BR /&gt;var sfOwnerEmail = event.inputFields['sf_owner_email'];&lt;BR /&gt;const sfOwnerName = event.inputFields['prospect_owner__sf_'];&lt;BR /&gt;&lt;BR /&gt;console.log("Salesforce Owner Email ::::::" + sfOwnerEmail);&lt;BR /&gt;console.log("Salesforce Owner Name ::::::" + sfOwnerName);&lt;BR /&gt;&lt;BR /&gt;if(sfOwnerName == "abc"){&lt;BR /&gt;sfOwnerEmail = "test@abc.com";&lt;BR /&gt;}&lt;BR /&gt;else if(sfOwnerName == "xyz"){&lt;BR /&gt;sfOwnerEmail = "test1@abc.com";&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;try {&lt;/P&gt;&lt;P&gt;//5. Sending the request to HS Owner API to get desired HS User details.&lt;BR /&gt;const response = await hubspotClient.apiRequest({&lt;BR /&gt;method: 'get',&lt;BR /&gt;path: '/owners/v2/owners',&lt;BR /&gt;qs: {email: sfOwnerEmail}&lt;BR /&gt;})&lt;BR /&gt;console.log("Response JSON String ::::::" + JSON.stringify(response.body, null, 2));&lt;BR /&gt;&lt;BR /&gt;var i;&lt;BR /&gt;loop1:&lt;BR /&gt;for(i=0; i &amp;lt; response.body.length; i++){&lt;BR /&gt;for(var propertyKey in response.body[i]) {&lt;BR /&gt;// console.log("key:"+propertyKey+", propertyValue:"+response.body[i][propertyKey]);&lt;BR /&gt;if(propertyKey == "type" &amp;amp;&amp;amp; response.body[i][propertyKey] == "PERSON"){&lt;BR /&gt;break loop1;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;//6.. Extracting the first PERSON OwnerId property from response JSON&lt;BR /&gt;var ownerId = response.body[i].ownerId;&lt;BR /&gt;var ownerEmail = response.body[i].email;&lt;BR /&gt;&lt;BR /&gt;console.log("OwnerId PropertyValue::::::" + ownerId);&lt;BR /&gt;console.log("Email Property::::::" + ownerEmail);&lt;/P&gt;&lt;P&gt;//7. Updating the Contact that triggered the workflow, with OwnerId to assign owner.&lt;BR /&gt;const apiResponse = await hubspotClient.crm.contacts.basicApi.update(event.object.objectId, {&lt;BR /&gt;"properties": {&lt;BR /&gt;"hubspot_owner_id": ownerId&lt;BR /&gt;}&lt;BR /&gt;})&lt;BR /&gt;&lt;BR /&gt;}&lt;BR /&gt;catch (e) {&lt;BR /&gt;e.message === 'HTTP request failed' ? console.error(JSON.stringify(e.response, null, 2)) : console.error(e)&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;}&lt;/P&gt;</description>
    <pubDate>Thu, 27 Apr 2023 16:59:17 GMT</pubDate>
    <dc:creator>DSaini1</dc:creator>
    <dc:date>2023-04-27T16:59:17Z</dc:date>
    <item>
      <title>Need to understand custom code</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Need-to-understand-custom-code/m-p/787350#M63559</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;//1. Import required libraries&lt;BR /&gt;const hubspot = require('@hubspot/api-client'); // HubSpot Node Client will allow us to make calls to HubSpot API&lt;BR /&gt;&lt;BR /&gt;exports.main = async (event, callback) =&amp;gt; {&lt;BR /&gt;&lt;BR /&gt;const hubspotClient = new hubspot.Client({"accessToken":"pat-na1-XXXXXXXXXXXXXXXX"});&lt;/P&gt;&lt;P&gt;//2. Get email address of Contact (Prospect in Salesforce) Owner assigned by SF in a variable&lt;BR /&gt;var sfOwnerEmail = event.inputFields['sf_owner_email'];&lt;BR /&gt;const sfOwnerName = event.inputFields['prospect_owner__sf_'];&lt;BR /&gt;&lt;BR /&gt;console.log("Salesforce Owner Email ::::::" + sfOwnerEmail);&lt;BR /&gt;console.log("Salesforce Owner Name ::::::" + sfOwnerName);&lt;BR /&gt;&lt;BR /&gt;if(sfOwnerName == "abc"){&lt;BR /&gt;sfOwnerEmail = "test@abc.com";&lt;BR /&gt;}&lt;BR /&gt;else if(sfOwnerName == "xyz"){&lt;BR /&gt;sfOwnerEmail = "test1@abc.com";&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;try {&lt;/P&gt;&lt;P&gt;//5. Sending the request to HS Owner API to get desired HS User details.&lt;BR /&gt;const response = await hubspotClient.apiRequest({&lt;BR /&gt;method: 'get',&lt;BR /&gt;path: '/owners/v2/owners',&lt;BR /&gt;qs: {email: sfOwnerEmail}&lt;BR /&gt;})&lt;BR /&gt;console.log("Response JSON String ::::::" + JSON.stringify(response.body, null, 2));&lt;BR /&gt;&lt;BR /&gt;var i;&lt;BR /&gt;loop1:&lt;BR /&gt;for(i=0; i &amp;lt; response.body.length; i++){&lt;BR /&gt;for(var propertyKey in response.body[i]) {&lt;BR /&gt;// console.log("key:"+propertyKey+", propertyValue:"+response.body[i][propertyKey]);&lt;BR /&gt;if(propertyKey == "type" &amp;amp;&amp;amp; response.body[i][propertyKey] == "PERSON"){&lt;BR /&gt;break loop1;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;//6.. Extracting the first PERSON OwnerId property from response JSON&lt;BR /&gt;var ownerId = response.body[i].ownerId;&lt;BR /&gt;var ownerEmail = response.body[i].email;&lt;BR /&gt;&lt;BR /&gt;console.log("OwnerId PropertyValue::::::" + ownerId);&lt;BR /&gt;console.log("Email Property::::::" + ownerEmail);&lt;/P&gt;&lt;P&gt;//7. Updating the Contact that triggered the workflow, with OwnerId to assign owner.&lt;BR /&gt;const apiResponse = await hubspotClient.crm.contacts.basicApi.update(event.object.objectId, {&lt;BR /&gt;"properties": {&lt;BR /&gt;"hubspot_owner_id": ownerId&lt;BR /&gt;}&lt;BR /&gt;})&lt;BR /&gt;&lt;BR /&gt;}&lt;BR /&gt;catch (e) {&lt;BR /&gt;e.message === 'HTTP request failed' ? console.error(JSON.stringify(e.response, null, 2)) : console.error(e)&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;}&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2023 16:59:17 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Need-to-understand-custom-code/m-p/787350#M63559</guid>
      <dc:creator>DSaini1</dc:creator>
      <dc:date>2023-04-27T16:59:17Z</dc:date>
    </item>
    <item>
      <title>Re: Need to understand custom code</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Need-to-understand-custom-code/m-p/787833#M63568</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/560368"&gt;@DSaini1&lt;/a&gt;&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":waving_hand:"&gt;👋&lt;/span&gt; Thanks for your post. Out of curiosity, is this code that you inherited?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a high-level view of what this code is set up to do:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;SPAN&gt;The code starts by importing the required libraries to make API calls to HubSpot from Node.js&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;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&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;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&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;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&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;SPAN&gt;I hope this helps get you moving forward.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Have fun building! — Jaycee&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2023 16:51:20 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Need-to-understand-custom-code/m-p/787833#M63568</guid>
      <dc:creator>Jaycee_Lewis</dc:creator>
      <dc:date>2023-04-28T16:51:20Z</dc:date>
    </item>
  </channel>
</rss>

