⚙ Operations Hub

NicolasF
Participant

Custom Code action not copying into Custum Property

SOLVE

Hi everyone !

 

I recently started to use operation pro for one of my account and I ran into a simple error I'm not sure how to fix.

 

The goal was to create a numerical order for transactions created through the pre-order of a specific item via the Shopify integration. To put it simply, the first contact to pre-order the product would have its transaction be given the number 1, the second contact's transaction would be given number 2, and so on. This was important for our client as they want to respect that order when they'll finally have the product available.

 

I've built a workflow using some simple triggers which works fine. I then created a custom code object. Here is my code :

 

exports.main = (event, callback) => {

callback({
outputFields: {
ordre: event.object.objectId % 100000
}
});
}

 

I'm not a coding expert, but essentially, it should give the reminder so the transaction should be given the numbers 0, then 1, then 2,etc. (I chose 100000) because we know we won't be selling that much products, to make sure the loop doesn't start back from 0.

That part seems to work fine as well.

 

However, following the tutorials on Hubspot Academy, I created a custom transaction property (set as a number) to receive the data of the calculation, but that part of the workflow always fails.

 

Here are some screenshot (the customer's portal is in french, but it can give you an idea).

Could someone help me with that ?

 

Here are some screenshots.screencapture-app-hubspot-workflows-8998219-platform-flow-70106418-history-log-events-2021-05-12-14_12_52.pngscreencapture-app-hubspot-workflows-8998219-platform-flow-70106418-edit-actions-1-custom-code-2021-05-12-14_12_07.png

 

Thanks !

0 Upvotes
1 Accepted solution
KyleJepson
Solution
Inbound Professor
Inbound Professor

Custom Code action not copying into Custum Property

SOLVE

So I just tried it, and it worked. Here's what I did:

 

  1. Create a custom deal property called "Ordre Numérique" and make it a number property. (Seems like you already have that step done.)
  2. Create your test deal and set it's "Ordre Numérique" to wherever you want the counting to start, minus one. (So if you want to start at 1, make it 0. If you already have a list of IDs that you want to add to, make it whatever the last one of those is.) For my example, I set the value to 607 (I chose this number at random), so we should expect the first deal to go through the workflow to get a value of 608 (and it did!)
  3. Copy the ID of the test deal (you can get that from the URL when you're on the deal record).
  4. Create a deal-based workflow (you can use the one you already have) and set up your enrollment triggers (you've already done this). Then add a custom code action and write your code. Here's the code I came up with:

 

const hubspot = require('@hubspot/api-client');

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

function processEvent(event) {
  const hubspotClient = new hubspot.Client({
    apiKey: process.env.HAPIKEY
  });
  
  let dealId = event.object.objectId;
  hubspotClient.crm.deals.basicApi.getById("5257315659", ["ordre_numerique"])
    .then(results => {
      let last_order = results.body.properties.ordre_numerique;
    let current_order = ++last_order;
    hubspotClient.crm.deals.basicApi.update(
      dealId,
      {properties: {["ordre_numerique"]: current_order}}
    )
 hubspotClient.crm.deals.basicApi.update(
      "5257315659",
      {properties: {["ordre_numerique"]: current_order}}
    )    
    })
}

 

 

Now, don't just copy-paste this code and expect it to work! Here are some notes:

  1. process.env.HAPIKEY refers to a secret I created called HAPIKEY that has my HubSpot API key stored in it. You'll need to get your API key (video explainer) and store it in a secret and then replace HAPIKEY with whatever you call your secret (unless you call it HAPIKEY).
  2. 5257315659 is the ID for the test deal I created. Your test deal will have its own ID. Whatever that ID is, put it in both places you see 5257315659 in this code.
  3. ordre_numerique is the internal name for the custom property I created. I'm guessing that'll be the same for you, but you should double check first, just in case.

And that's it. I've run three deals through this workflow in my own account, and the first one was given number 608, then the next one was given 609, and the third one was giving 610, which I think is exactly what you were going for.

 

Let me know if you have any trouble with it!

 

Kyle

View solution in original post

10 Replies 10
remingtonbegg
Contributor | Elite Partner
Contributor | Elite Partner

Custom Code action not copying into Custum Property

SOLVE

Hello @NicolasF 
You're going to need to reference something outside the workflow to make it increment. Problem is that workflow is only able to reference itself, not the number of times the workflow has run for instance. I'd recommend hitting something like zapier and referencing the number and incrementing with each workflow hit. You can make zapier return the contact field number incremented as 1 that way. (You could also do this with a serverless function but that would require CMS enterprise) not sure if you have that.

KyleJepson
Inbound Professor
Inbound Professor

Custom Code action not copying into Custum Property

SOLVE

I thought some kind of external storage would be required. A potentially hacky workaround is that you could have a dummy record somewhere (a deal would make sense, but it could be a contact or company or ticket or really anything) that has a number property on it that stores the property. Then you could create custom code that would fetch the number, increment it by one, and update that record again. That would save you the trouble of using an external system. The only downside is that you'd then have to have one random deal (or contact or company) sitting around, and if anyone deleted it (or manually changed the value of that property), that would be a problem.

 

@remingtonbegg -- what do you think of this idea?

0 Upvotes
KyleJepson
Solution
Inbound Professor
Inbound Professor

Custom Code action not copying into Custum Property

SOLVE

So I just tried it, and it worked. Here's what I did:

 

  1. Create a custom deal property called "Ordre Numérique" and make it a number property. (Seems like you already have that step done.)
  2. Create your test deal and set it's "Ordre Numérique" to wherever you want the counting to start, minus one. (So if you want to start at 1, make it 0. If you already have a list of IDs that you want to add to, make it whatever the last one of those is.) For my example, I set the value to 607 (I chose this number at random), so we should expect the first deal to go through the workflow to get a value of 608 (and it did!)
  3. Copy the ID of the test deal (you can get that from the URL when you're on the deal record).
  4. Create a deal-based workflow (you can use the one you already have) and set up your enrollment triggers (you've already done this). Then add a custom code action and write your code. Here's the code I came up with:

 

const hubspot = require('@hubspot/api-client');

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

function processEvent(event) {
  const hubspotClient = new hubspot.Client({
    apiKey: process.env.HAPIKEY
  });
  
  let dealId = event.object.objectId;
  hubspotClient.crm.deals.basicApi.getById("5257315659", ["ordre_numerique"])
    .then(results => {
      let last_order = results.body.properties.ordre_numerique;
    let current_order = ++last_order;
    hubspotClient.crm.deals.basicApi.update(
      dealId,
      {properties: {["ordre_numerique"]: current_order}}
    )
 hubspotClient.crm.deals.basicApi.update(
      "5257315659",
      {properties: {["ordre_numerique"]: current_order}}
    )    
    })
}

 

 

Now, don't just copy-paste this code and expect it to work! Here are some notes:

  1. process.env.HAPIKEY refers to a secret I created called HAPIKEY that has my HubSpot API key stored in it. You'll need to get your API key (video explainer) and store it in a secret and then replace HAPIKEY with whatever you call your secret (unless you call it HAPIKEY).
  2. 5257315659 is the ID for the test deal I created. Your test deal will have its own ID. Whatever that ID is, put it in both places you see 5257315659 in this code.
  3. ordre_numerique is the internal name for the custom property I created. I'm guessing that'll be the same for you, but you should double check first, just in case.

And that's it. I've run three deals through this workflow in my own account, and the first one was given number 608, then the next one was given 609, and the third one was giving 610, which I think is exactly what you were going for.

 

Let me know if you have any trouble with it!

 

Kyle

NicolasF
Participant

Custom Code action not copying into Custum Property

SOLVE

Thanks Kyle ! I just did it on my end and it works just fine.

Very sharp indeed !

 

remingtonbegg
Contributor | Elite Partner
Contributor | Elite Partner

Custom Code action not copying into Custum Property

SOLVE

Smart @KyleJepson ! I'd probably lean into using HubDB for this vs a "record" but that's pretty sharp

0 Upvotes
KyleJepson
Inbound Professor
Inbound Professor

Custom Code action not copying into Custum Property

SOLVE

Hi Nicolas!

 

Thanks for writing this up. Now that I'm seeing the full situation, I don't think you actually need a custom code action for this. If your "Ordre Numérique" property is a number property, you can use the "Increase or Decrease Property Value" action:

 

KyleJepson_0-1620926911894.png

 

Set up this way, this will add 1 to "Ordre Numérique" each time the workflow is triggered. Which I think is what you're trying to do?

 

Let me know if I'm missing the point, here.

 

Kyle

NicolasF
Participant

Custom Code action not copying into Custum Property

SOLVE

Hi Kyle,

 

Thanks for your response. However, I tried that, and all it does and give everyone the number 1.

What we actually want to accomplish is to have the first transaction to enter the workflow to have the number 1, the second transaction to enter the workflow(that belongs to another contact) to have number 2, the 3rd to have number 3, and so on.

 

Is there a way to do with (or without) a custom code ?

 

Thanks again !

 

 

 

0 Upvotes
KyleJepson
Inbound Professor
Inbound Professor

Custom Code action not copying into Custum Property

SOLVE

Ohhh, that makes more sense to me. That sounds like a good use case for custom code to me, but I'm not clever enough to do it myself. The code you're using is very much like the code I use in the Academy lesson to divide records into groups. I don't think that's quite right for this case, though. What you need to do is have a number that increments upward and is available to all deals, and I'm just not sure the best place to store that.

 

George was right to tag Remmington. I'll see if I can find some other smart people to weigh in on this.

0 Upvotes
GeorgeBThomas
Guide | Diamond Partner
Guide | Diamond Partner

Custom Code action not copying into Custum Property

SOLVE

Hey @NicolasF,

I'm going to tag in @remingtonbegg here as he knows code like the back of his hand!

Plus, he was part of our HubSpot Operations Hub Masterclass

George B. Thomas

HubSpot Helper & Owner

Georeg B. Thomas, LLC

(252) 656-5950 | (330) 232-6117
george@georgebthomas.com
www.georgebthomas.com
NicolasF
Participant

Custom Code action not copying into Custum Property

SOLVE

Thanks George !

0 Upvotes