Hello, I would like to calculate the total amount in the field on the occasion (see attached screen), how can I do this?
calculate the total amount in the field
Collapse
X
-
You can probably use this formula: https://docs.espocrm.com/administrat...titysumrelated
I haven't use it so can't give you advice on how-to.
Edit: Thinking about this I decide to give it a try, took a few minutes to learn how to do it, anyway here how I did it.
Main entity: Contact
(Custom) Many-to-Many Entity: Assets
In Contact I create a field call, "totalAssets"
In Assets I create a field call, "assetValue"
The formula I end up using is:
// Calculate Total Assets
totalAsset = entity\sumRelated('assets', 'assetValue');
---
So it working all nice and good in that it calculate correctly based on my test, the issue here it doesn't refresh the data until I make edit to the Contact... so I think you would need to auto refresh some data using Workflow or you have to use beforesaveHook instead of formula (which is aftersave). Hook is a topic I'm too ignorant in though.
So I guess it not too hard to use the formula above, change assets to the Entity you want, and 'totalAsset' 'assetValue' to the field you want. In your case I think
Assets = ROZpoty
totalAsset = IDcisloUNIE
assetValue = celkova...Last edited by esforim; 09-20-2021, 06:50 AM.Comment
-
Hi, as espcrm correctly responded, one way to do this is via Formula - easier way for non programming users.
Another way is to write a custom code using afterSave() entity, which would automatically sum all values from ROZPOTYs and updated ID CISLO UNIE - after saving your ROZPOTYs record.
You can see here example code for recalculating Opportunity 'Amount' field everytime I add new or remove OpportunityProduct (custom entity) to the Opportunity:
Info: The method recalculateOpportunityPrice() is being called from afterSave() method on OpportunityProduct
PHP Code:/** * @param Entity $opportunity Opportunity that its Amount should be recalculated. * @returns void Since it only recalculates Amount on Opportunity, nothing is returned. */ private function recalculateOpportunityPrice(Entity $opportunity): void { $entityManager = $this->entityManager; $opportunityProducts = $entityManager ->getRepository($this->entityType) ->getRelation($opportunity, 'opportunityProducts') ->find(); $totalAmount = 0.00; foreach ($opportunityProducts as $opportunityProduct) { $price = $opportunityProduct->get('totalPrice'); $totalAmount += $price; } $opportunity->set('amount', $totalAmount); $entityManager->saveEntity($opportunity); }
Last edited by alter; 09-21-2021, 02:09 PM.Comment
Comment