# Calculated properties for Products

**URL:** <https://community.hubspot.com/t/calculated-properties-for-products/21266>\
**Category:** HubSpot Ideas\
**Tags:** products-line-items\
**Created:** [May 29, 2020, 4:38pm UTC](https://community.hubspot.com/t/calculated-properties-for-products/21266 "2020-05-29T16:38:56Z")\
**Posts on this page:** 1\
**Showing post:** 52

<div class="post-metadata">

**Author:** ![dsmarion](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dsmarion/32/7509_2.png) [@dsmarion](https://community.hubspot.com/u/dsmarion)\
**Post date:** [November 9, 2023, 12:28pm UTC](https://community.hubspot.com/t/calculated-properties-for-products/21266/52 "2023-11-09T12:28:22Z")

</div>

This idea is a needed one for sure. In the meantime you can do it with a workflow using a custom code action. Here is an example getting all the line items in a deal and then performing a calculation on each one and finally storing the result back in the line item - hope this helps you:

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

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

/\*\*\*\*\*

Use inputs to get data from any action in your workflow and use it in your code instead of having to use the HubSpot API.

\*\*\*\*\*/

let response = ‘’;

let errmsg = ‘’;

let associations = ‘’;

let lineitem = ‘’;

let my\_calculated\_field = 0;

let success = false;

const hubspotClient = new hubspot.Client({ accessToken: process.env.HS\_TOKEN });

try {

response = await hubspotClient.crm.deals.associationsApi.getAll(event.object.objectId, ‘line\_item’);

if (response) {

associations = response.results;

for (let i=0; i\<associations.length; i++) {

let properties = [“hs\_cost\_of\_goods\_sold, quantity”];

lineitem = await hubspotClient.crm.lineItems.basicApi.getById(associations[i].toObjectId, properties);

// do your calculation here

my\_calculated\_field = lineitem.properties.hs\_cost\_of\_goods\_sold \* lineitem.properties.quantity;

properties = {

“my\_calculated\_field” : my\_calculated\_field ,

};

const prop\_bag = { properties };

const lineItemId = lineitem.id;

const idProperty = undefined;

await hubspotClient.crm.lineItems.basicApi.update(lineItemId, prop\_bag, idProperty);

success = true;

}

} catch (e) {

errmsg += "AN ERROR OCCURRED! " + e.message;

}

/\*\*\*\*\*

Use the callback function to output data that can be used in later actions in your workflow.

\*\*\*\*\*/

callback({

outputFields: {

associations: JSON.stringify(associations, null, 2),

errmsg: errmsg,

lineitem: JSON.stringify(lineitem, null, 2),

my\_calculated\_field : my\_calculated\_field ,

success: success

}

});

}

---

_[View the full topic](https://community.hubspot.com/t/calculated-properties-for-products/21266)._
