Hello everyone, I ask you for help for this need.
In the contracts module I have created custom fields called “Total”, “Advance Payment” and “Balance”.
Since the customer can split the payment however he likes, I have created a custom module form for payments.
I need to sum the customer’s payments and subtract it from the balance field.
I tried via Workflow module without success, maybe someone can drive me in a snippet of php?
Many thanks,
A.
Could you please post here how you’ve created the workflow? Reading your post, it should work.
HI @mtierno, thanks for asking,
I didn’t made the workflow because I think it is impossible to implement between two different modules, or am I wrong?
Anyway I attached a screenshoot of my need.
I need to sum a filed of multiple records in a custom module and then subtract the summation from the total field in contracts module.
Oh, I see. Indeed, it’s possible to do calculations with fields from related modules, use fórmula and get related field as related modules parameters.
In your case, however, you are gonna need to code a logic Hook.
In /custom/modules/youmodule you need to change or create the logic_hooks.php file and insert an before_save Hook, say, sum_fields.php.
Then you need to create the sum_fields.php file and implement.
Try to check logic hooks in documentation.
If you need, I can post a sample here later today.
Hi @mtierno, thanks for the tips.
I’m gonna read documentation right now.
I really appreciate if you can share with me a sample when you can.
Thanks a lot.
In the module you want to calculate the field, go to /custom/modules/your_module and add the lines below to the logic_hooks.php file. If it doesn’t exist, create it (attention to file permissions!):
$hook_array['after_save'][] = Array(1, 'sumFields', 'custom/modules/your_module/yoursumfiledfile.php','addTotal', 'sumFields');
Then, in the same directory, create the file yoursumfiledfile.php
as below:
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class addTotal {
function sumFields(&$focus, $event, $arguments) {
$the_sum = 0;
$items = $focus->get_linked_beans(
'nome_of_the_related_module_relationship',
'nome_of_the_related_module_relationship',
array(),
0,
1000, // the nuber of records to retrieve
0,
'');
for($x = 0; $x < count($items); $x++) {
$the_sum += $items[$x]->the_related_field_to_sum ;
} ;
$focus->the_field_ to_store_the_sum;
}
}
?>
Hope it works. You may have to fight a little bit. The relationship in this case is 1 to many.