We use cookies to make HubSpot's community a better place. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. To learn more, and to see a full list of cookies we use, check out our Cookie Policy (baked goods not included).
May 12, 2021 2:20 PM - edited May 13, 2021 1:28 PM
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.
Thanks !
Solved! Go to Solution.
May 13, 2021 3:46 PM - edited May 13, 2021 3:47 PM
So I just tried it, and it worked. Here's what I did:
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:
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
May 13, 2021 2:25 PM
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.
May 13, 2021 2:59 PM
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?
May 13, 2021 3:46 PM - edited May 13, 2021 3:47 PM
So I just tried it, and it worked. Here's what I did:
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:
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
May 17, 2021 10:59 AM
Thanks Kyle ! I just did it on my end and it works just fine.
Very sharp indeed !
May 14, 2021 7:21 AM
Smart @KyleJepson ! I'd probably lean into using HubDB for this vs a "record" but that's pretty sharp
May 13, 2021 1:29 PM
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?
Let me know if I'm missing the point, here.
Kyle
May 13, 2021 1:49 PM
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 !
May 13, 2021 2:07 PM
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.
May 13, 2021 12:57 PM
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.
| ||||||||||||||
|
May 13, 2021 1:49 PM
Thanks George !