⚙ Operations Hub

mbarba
HubSpot Employee
HubSpot Employee

Use Case: Custom Code Actions at Work

Community Post_ OH_0015 Canva Graphic.png

 

-----

 

We have someone who shared the following success story in leveraging Custom Code Actions’ 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 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?

7 Replies 7
nikodev
Top Contributor | Elite Partner
Top Contributor | Elite Partner

Use Case: Custom Code Actions at Work

@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?  

A8 Labs

0 Upvotes
KBansal
Member

Use Case: Custom Code Actions at Work

@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. 

0 Upvotes
nikodev
Top Contributor | Elite Partner
Top Contributor | Elite Partner

Use Case: Custom Code Actions at Work

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: 

 

 

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

 

 

A8 Labs

KBansal
Member

Use Case: Custom Code Actions at Work

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" 

0 Upvotes
mbarba
HubSpot Employee
HubSpot Employee

Use Case: Custom Code Actions at Work

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?

nikodev
Top Contributor | Elite Partner
Top Contributor | Elite Partner

Use Case: Custom Code Actions at Work

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. 

A8 Labs

mbarba
HubSpot Employee
HubSpot Employee

Use Case: Custom Code Actions at Work

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

0 Upvotes