top of page

Total quantity field calculation

Updated: Dec 14, 2021

Many clients asked me this question:

How can I calculate the total quantity from child entity's quantity field

I am going to write the formula based on this structure:


This is the formula:


// Calculate the line "Total Price"
totalPrice = quantity * unitPrice;

// Calculate the parent order "Total Price"
ifThen(
    entity\isAttributeChanged('totalPrice'),
    
    $priceDiff= totalPrice - entity\attributeFetched('totalPrice');

    record\update(
        'Order', orderId,
        'totalPrice', order.totalPrice + $priceDiff
    );
);

Tell me in comments if this was helpful or if any question or suggesion

666 views8 comments

8 Comments


Unknown member
Dec 13, 2021

When do you use the above code instead of the "entity\sumRelated" function to re-calculate totalPrice in the Order entity?

Like
Unknown member
Dec 16, 2021
Replying to

Thanks for your reply. I am trying to understand your workaround.

I see that you have a price difference field to use for the comparison, but trying to understand why that is required. Also, you have two "totalPrice" fields in different tables, which adds to my confusion. 🤔

Like

Is option to do it using dynamic-handler? https://docs.espocrm.com/development/dynamic-handler/

Like
Unknown member
Dec 14, 2021
Replying to

Sorry but question is not clear enough

Like

It work's! Thank You! Is some simple way to see result of calculation "live", before clicking "update"?

Like
Unknown member
Dec 14, 2021
Replying to

You are welcome,

To have live sum result will need custom view and some js code

Like
bottom of page