# Data Output Error Even Though It's Defined

**URL:** <https://community.hubspot.com/t/data-output-error-even-though-its-defined/138321>\
**Category:** Data Hub\
**Created:** [August 20, 2025, 7:59pm UTC](https://community.hubspot.com/t/data-output-error-even-though-its-defined/138321 "2025-08-20T19:59:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JoeBurchard](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/joeburchard/32/52764_2.png) [@JoeBurchard](https://community.hubspot.com/u/JoeBurchard)\
**Post date:** [August 20, 2025, 7:59pm UTC](https://community.hubspot.com/t/data-output-error-even-though-its-defined/138321/1 "2025-08-20T19:59:35Z")

</div>

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:

1. Created a private app to view line items, products, deals and edit deals
2. Created a property \<def\_amount\> that is a currency property
3. Defined def\_amount
4. 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?

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

const MATCH\_VALUE = ‘DEFENDIFY’;  
const PRODUCT\_TYPE\_PROP = ‘hs\_product\_type’;

// Net total preference order  
const NET\_PRIMARY = ‘amount’; // HubSpot line-item net total  
const NET\_FALLBACK = ‘hs\_total\_amount’; // fallback if present

// Fallback compute fields  
const PRICE\_PROPS = [‘price’, ‘hs\_price’];  
const QTY\_PROP = ‘quantity’;  
const DISC\_AMT\_PROP = ‘hs\_discount\_amount’;  
const DISC\_PCT\_PROP = ‘hs\_discount\_percentage’;

const normalize = v =\> String(v ?? ‘’).trim().toUpperCase();  
const toNumber = v =\> {  
if (v == null) return 0;  
const n = parseFloat(String(v).replace(/[^0-9.\-]/g, ‘’));  
return Number.isFinite(n) ? n : 0;  
};

exports.main = async (event) =\> {  
const client = new hubspot.Client({ accessToken: process.env.DEFENDIFY\_CALC });  
const dealId = event.object.objectId;

try {  
const deal = await client.crm.deals.basicApi.getById(dealId, undefined, undefined, [‘line\_items’]);  
const lineItemIds = (deal?.associations?.line\_items?.results || []).map(x =\> String(x.id)).filter(Boolean);  
if (lineItemIds.length === 0) return { outputFields: { def\_amount: 0 } };

const propsToFetch = [  
PRODUCT\_TYPE\_PROP, NET\_PRIMARY, NET\_FALLBACK,  
…PRICE\_PROPS, QTY\_PROP, DISC\_AMT\_PROP, DISC\_PCT\_PROP,  
];  
const batch = await client.crm.lineItems.batchApi.read({  
properties: propsToFetch,  
inputs: lineItemIds.map(id =\> ({ id })),  
});

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

sum += net;  
}

return { outputFields: { def\_amount: sum } };  
} catch {  
return { outputFields: { def\_amount: 0 } };  
}  
};

---

<div class="post-metadata">

**Author:** ![JoeBurchard](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/joeburchard/32/52764_2.png) [@JoeBurchard](https://community.hubspot.com/u/JoeBurchard)\
**Post date:** [August 20, 2025, 10:07pm UTC](https://community.hubspot.com/t/data-output-error-even-though-its-defined/138321/2 "2025-08-20T22:07:52Z")

</div>

Below is an image of the output

 ![Screenshot 2025-08-20 155916.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/e/9/e9fd66fc1fed1ebec0aa275cb06bfdeb03455441.png)

---

<div class="post-metadata">

**Author:** ![KareemT](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kareemt/32/55956_2.png) [@KareemT](https://community.hubspot.com/u/KareemT)\
**Post date:** [August 27, 2025, 6:00pm UTC](https://community.hubspot.com/t/data-output-error-even-though-its-defined/138321/3 "2025-08-27T18:00:15Z")

</div>

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

 ![154200iC8FD68CC263500AC.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/a/5/a5dc0f989ca000510fd0c24a15426296932660fe.png)

---

<div class="post-metadata">

**Author:** ![RubenBurdin](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/rubenburdin/32/75212_2.png) [@RubenBurdin](https://community.hubspot.com/u/RubenBurdin)\
**Post date:** [December 16, 2025, 6:52pm UTC](https://community.hubspot.com/t/data-output-error-even-though-its-defined/138321/4 "2025-12-16T18:52:00Z")

</div>

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 ([Workflows | Custom Code Actions - HubSpot docs](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.
