# Use Case: Custom Code Actions at Work

**URL:** <https://community.hubspot.com/t/use-case-custom-code-actions-at-work/38947>\
**Category:** Data Hub\
**Created:** [June 15, 2021, 2:08pm UTC](https://community.hubspot.com/t/use-case-custom-code-actions-at-work/38947 "2021-06-15T14:08:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbarba](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mbarba/32/31928_2.png) [@mbarba](https://community.hubspot.com/u/mbarba)\
**Post date:** [June 15, 2021, 2:08pm UTC](https://community.hubspot.com/t/use-case-custom-code-actions-at-work/38947/1 "2021-06-15T14:08:56Z")

</div>

![Community Post_ OH_0015 Canva Graphic.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/d/8/d832810b14f70270b1da4fbd7584e4cd449520f4.png)

-----

**We have someone who shared the following success story in leveraging [Custom Code Actions’ supported libraries](https://knowledge.hubspot.com/workflows/use-custom-code-actions-in-workflows#supported-libraries) to accomplish a goal they couldn’t otherwise:**

“We needed a way to call a specific endpoint which is owned by a 3rd party vendor, they were unable to make a customization to accept the webhook feature, so this was exactly what we needed. We were able to use the workflow to set some properties on the contact and then ship that information off to the vendor to create a user in their system using the [Axios library](https://www.npmjs.com/package/axios) in the custom code feature. It was nice having a few JavaScript libraries pre-installed to work with. As of now, this is a pretty simple use case as we are only making one simple POST request, but we are planning to build some error handling to surround it.”

**What have you built so far using Custom Code Actions?**

---

<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:** [June 15, 2021, 3:44pm UTC](https://community.hubspot.com/t/use-case-custom-code-actions-at-work/38947/2 "2021-06-15T15:44:53Z")

</div>

We created a Custom Code Action at Aptitude 8 to help facilitate some internal processes.  
**Use case**

1. Problem
  1. Projects (custom object) are syncing in from SFDC, but they weren’t associated with users and didn’t have owners.

2. Business Case
  1. We wanted to be able to build dashboards inside of Hubspot based on the owner of project records so we could have some visibility on the project ownders

3. Solution
  1. We built a custom coded action that runs on Projects to “associate” a Project to a user using the “Project Owner” HubSpot User property, which is set based on the “Project Lead Email” property. This property contains the email address of the owner.
  2. Hit the owner endpoint to find and set the “Project Owner” property, by checking for a user with an email matching the value in the “Project Lead Email” property.

**Custom Code Action:**

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

exports.main = (event, callback) => {
 return callback(processEvent(event));
};

let projectRes;
let leadEmail;
let ownerRes;
let ownerId;
let updateProjectRes;

function processEvent(event) {
 const hubspotClient = new hubspot.Client({
 apiKey: process.env.key,
 });
 (async function() {
 try {
 projectRes = await hubspotClient.apiRequest({
 method: "GET",
 path: `/crm/v3/objects/2-1569717/${event.object.objectId}`,
 headers: {
 accept: "application/json",
 "content-type": "application/json",
 },
 qs: {
 properties: "project_lead_email",
 },
 });

 leadEmail = projectRes.body.properties.project_lead_email;

 ownerRes = await hubspotClient.apiRequest({
 method: "GET",
 path: `/crm/v3/owners/`,
 headers: {
 accept: "application/json",
 "content-type": "application/json",
 },
 qs: {
 email: `${leadEmail}`,
 archived: "false",
 },
 });
 console.log("OWNER HERE", ownerRes.body.results[0].id);
 ownerId = ownerRes.body.results[0].id;

 updateProjectRes = await hubspotClient.apiRequest({
 method: "PATCH",
 path: `/crm/v3/objects/2-1569717/${event.object.objectId}`,
 headers: {
 accept: "application/json",
 "content-type": "application/json",
 },
 body: {
 properties: { project_owner: `${ownerId}` },
 },
 });
 console.log("final", updateProjectRes.body);
 } catch (err) {
 console.log("ERROR HERE", err);
 }
 })();
}

```

 ![447608_1_Screen Shot 2021-06-15 at 10.20.22 AM.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/e/e/ee8cc83e083ca8b372600f61b151030d19693cc8.png)

---

<div class="post-metadata">

**Author:** ![mbarba](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mbarba/32/31928_2.png) [@mbarba](https://community.hubspot.com/u/mbarba)\
**Post date:** [June 16, 2021, 5:38pm UTC](https://community.hubspot.com/t/use-case-custom-code-actions-at-work/38947/3 "2021-06-16T17:38:53Z")

</div>

Hi @nikodev! I am loving this! Thank you so much for sharing. Your breakdown of the problem, business case, and solution made it easy to understand the value of the custom code you wrote. Do you usually find your custom code action ideas after you’ve manually completed a certain action multiple times and so, therefore, you are looking for a way to automate it? Do you notice a pattern like this on how these ideas normally arise for you?

---

<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:** [July 23, 2021, 2:55pm UTC](https://community.hubspot.com/t/use-case-custom-code-actions-at-work/38947/4 "2021-07-23T14:55:51Z")

</div>

Hi there @mbarba. I’m really late on replying here, and for that I apologize! I’m happy to hear it was understandable.  
To answer your question - I work on the RevOps side of our company, heading up our development work there. I work alongside an incredibly talented and knowledgeable group of RevOps Consultants that know HubSpot inside and out.  
When they have a problem that can’t be solved with HubSpot out of the box, I’m pulled in. The combination of their understanding of the system and my knowledge of code and the HubSpot APIs enables us to come up with some pretty interesting, creative solutions with the help of Operations Hub.  
Operations Hub and more specifically, custom code actions has broadened the realm of possibility in HubSpot to a truly tremendous extent. Exciting times within the ecosystem.

---

<div class="post-metadata">

**Author:** ![mbarba](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mbarba/32/31928_2.png) [@mbarba](https://community.hubspot.com/u/mbarba)\
**Post date:** [July 27, 2021, 12:58pm UTC](https://community.hubspot.com/t/use-case-custom-code-actions-at-work/38947/5 "2021-07-27T12:58:02Z")

</div>

@nikodev That is absolutely fascinating. Thank you so much for sharing!

---

<div class="post-metadata">

**Author:** ![KBansal](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kbansal/32/13521_2.png) [@KBansal](https://community.hubspot.com/u/KBansal)\
**Post date:** [July 19, 2021, 7:13pm UTC](https://community.hubspot.com/t/use-case-custom-code-actions-at-work/38947/6 "2021-07-19T19:13:24Z")

</div>

Hi @nikodev , Did the above snippet work for you? I’m also developing a custom code action and have to rely heavily on async/await. The workflow editor keeps giving me a parsing error on the async.

I tried pasting your code into the editor and it gave me the same error on line 18:7 “Parsing error: Unexpected token function”

---

<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:** [July 23, 2021, 2:46pm UTC](https://community.hubspot.com/t/use-case-custom-code-actions-at-work/38947/7 "2021-07-23T14:46:13Z")

</div>

@KBansal It did work. However, I get the same error as you do for some reason. But it doesn’t prevent me from saving and running the code within the editor, which is a bit confusing. So while the error is there, it doesn’t seem to have any impact on the functionality of the code. As a result, I’ve assumed it’s a bug of sorts.  
Have you tried saving and running anyway?

---

<div class="post-metadata">

**Author:** ![KBansal](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kbansal/32/13521_2.png) [@KBansal](https://community.hubspot.com/u/KBansal)\
**Post date:** [July 23, 2021, 5:56pm UTC](https://community.hubspot.com/t/use-case-custom-code-actions-at-work/38947/8 "2021-07-23T17:56:12Z")

</div>

@nikodev I forgot to reply but yes it did work. It was a little annoying so I just started to code locally and making sure it works in node 12.0.0.
