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 :
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).
Custom Code action not copying into Custum Property
SOLVE
So I just tried it, and it worked. Here's what I did:
Create a custom deal property called "Ordre Numérique" and make it a number property. (Seems like you already have that step done.)
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!)
Copy the ID of the test deal (you can get that from the URL when you're on the deal record).
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:
Now, don't just copy-paste this code and expect it to work! Here are some notes:
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).
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.
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.
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.
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.
Custom Code action not copying into Custum Property
SOLVE
So I just tried it, and it worked. Here's what I did:
Create a custom deal property called "Ordre Numérique" and make it a number property. (Seems like you already have that step done.)
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!)
Copy the ID of the test deal (you can get that from the URL when you're on the deal record).
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:
Now, don't just copy-paste this code and expect it to work! Here are some notes:
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).
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.
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.
I tried to use the code but it does not work for me. I think becuase I'm using private apps and not API key. do you have any updates to implement the code with private apps?
I have posted this here before but have not received an answer yet.
Custom Code action not copying into Custum Property
SOLVE
Hi Ahmad,
Unfortunately, I haven't had a chance to play around with private apps yet, so I'm not confident on the best way to implement something like this now. Have you tried reaching out on the developer forums? developers.hubspot.com
I wanted to implement this but unfortunately it doesn't work, probably the reason is that API keys are no longer accessible and we need to implement the code with private apps, do you have an idea how we can do this with private apps?
I have posted this issue here but I didn't get any response yet.
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:
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?
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 ?
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.