top of page
Writer's pictureEymen Elkum

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

706 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

lukasz.serafin
May 12, 2021

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

lukasz.serafin
May 10, 2021

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