I am attempting to create a calculation on deal records that will sum the net amounts on all line items of a specific Product Type. Steps I've taken:
Created a private app to view line items, products, deals and edit deals
Created a property <def_amount> that is a currency property
Defined def_amount
Added def_amount as a data output
I'm not a dev so excuse the likely **bleep** code, but the code is below. When I test the action, I keep getting the error that def_amount is not defined in the code. What is going on?
// Net total preference order const NET_PRIMARY = 'amount'; // HubSpot line-item net total const NET_FALLBACK = 'hs_total_amount'; // fallback if present
let sum = 0; for (const row of (batch?.results || [])) { const p = row?.properties || {}; if (normalize(p[PRODUCT_TYPE_PROP]) !== MATCH_VALUE) continue;
let net = toNumber(p[NET_PRIMARY]); if (!(net > 0)) net = toNumber(p[NET_FALLBACK]);
if (!(net > 0)) { const qty = toNumber(p[QTY_PROP]) || 1; const unit = PRICE_PROPS.reduce((acc, k) => (acc > 0 ? acc : toNumber(p[k])), 0) || 0; const pre = unit * qty; const discAmt = toNumber(p[DISC_AMT_PROP]); const discPct = toNumber(p[DISC_PCT_PROP]); const totalDisc = discAmt > 0 ? discAmt * qty : (discPct > 0 ? pre * (discPct / 100) : 0); net = Math.max(0, pre - totalDisc); }
I'm not a developer myself, I wish I could help with the code 😄
But, have you tried a rollup field instead of a code? That seems to be a much easier solution. Should look like this on the deal level.
While you can't add line item name as a condition (which I find weird), perhaps if you scroll through the options available, you'll find a fit identifier or even create a custom identifier.
Did my post help answer your query? Help the community by marking it as a solution.
Hey @JoeBurchard , the code you pasted looks fine for returning an output, so when HubSpot says def_amount is not defined, it’s usually not complaining about your math. It’s complaining about a mismatch between what HubSpot thinks the output is called and what your code returns.
In custom code actions, the output field “name” is not your deal property label and it’s not the display name you see in the UI. HubSpot uses the output field’s internal name, and it has to match the key you return in outputFields exactly (including case, no spaces).
So double check the output definition in the action: if the output is actually def_amount_1, def_amount__c, or has a trailing space, returning { def_amount: sum } will throw weird “not defined” style errors even though your JS object is valid. This is the doc section that describes the output contract (https://developers.hubspot.com/docs/api-reference/automation-actions-v4-v4/custom-code-actions )
One more gotcha: you named the deal property <def_amount> in your post. If you literally used angle brackets anywhere (property name, output name, or mapping), HubSpot will sanitize the internal name and it won’t be what you expect. The safe path is: confirm the deal property internal name in settings, then confirm the custom code action output internal name, then make them match exactly.
Small debugging tip: change your catch to catch (e) { console.log(e); return { outputFields: { def_amount: 0 } }; } so you can see the real runtime error in the test output.
Quick question: is the error happening in the custom code action test run, or only when you map the output to the deal property afterward? That determines whether it’s an output-name mismatch or a mapping issue.
Did my answer help? Please mark it as a solution to help others find it too.
Ruben Burdin HubSpot Advisor Founder @ Stacksync Real-Time Data Sync between any CRM and Database
Hey @JoeBurchard , the code you pasted looks fine for returning an output, so when HubSpot says def_amount is not defined, it’s usually not complaining about your math. It’s complaining about a mismatch between what HubSpot thinks the output is called and what your code returns.
In custom code actions, the output field “name” is not your deal property label and it’s not the display name you see in the UI. HubSpot uses the output field’s internal name, and it has to match the key you return in outputFields exactly (including case, no spaces).
So double check the output definition in the action: if the output is actually def_amount_1, def_amount__c, or has a trailing space, returning { def_amount: sum } will throw weird “not defined” style errors even though your JS object is valid. This is the doc section that describes the output contract (https://developers.hubspot.com/docs/api-reference/automation-actions-v4-v4/custom-code-actions )
One more gotcha: you named the deal property <def_amount> in your post. If you literally used angle brackets anywhere (property name, output name, or mapping), HubSpot will sanitize the internal name and it won’t be what you expect. The safe path is: confirm the deal property internal name in settings, then confirm the custom code action output internal name, then make them match exactly.
Small debugging tip: change your catch to catch (e) { console.log(e); return { outputFields: { def_amount: 0 } }; } so you can see the real runtime error in the test output.
Quick question: is the error happening in the custom code action test run, or only when you map the output to the deal property afterward? That determines whether it’s an output-name mismatch or a mapping issue.
Did my answer help? Please mark it as a solution to help others find it too.
Ruben Burdin HubSpot Advisor Founder @ Stacksync Real-Time Data Sync between any CRM and Database
I'm not a developer myself, I wish I could help with the code 😄
But, have you tried a rollup field instead of a code? That seems to be a much easier solution. Should look like this on the deal level.
While you can't add line item name as a condition (which I find weird), perhaps if you scroll through the options available, you'll find a fit identifier or even create a custom identifier.
Did my post help answer your query? Help the community by marking it as a solution.