Sum of fields on a related module - Workflow or Logic hook?

Ok so I have a list of of custom modules that kind of play into this here. Clients, Authorized Services and Client Notes. 1 Client can have many Authorized Services. Each Authorized Service can have Many Client notes.

On the Client Notes there is a field that is essentially the length of time spent on that note - this already calculates on a workflow set on Client Notes when a record is created or modified. This field is called Units. On the Authorized Services I have a running unit balance, Adding in those units and such will be an easy step once I get this part figured out, but for this purpose lets say:

AuthorizedService.CurrentWorkedUnitBalance = 100.

We enter 5 Client Notes under this one Authorized Service as follows
Note 1 = 5 units
Note 2 = 3 Units
Note 3 = 15 Units
Note 4 = 2 Units
Note 5 = 1 Unit

I am needing to take the notes per Service and get an updated running total on the Authorized Service, so after each note the Current unit balance would go to:

From there - more logic gets applied… and I’m pretty sure I have a path there… my question here is - Can I have a workflow that has an action of ClientNote record created or modified that will update this running balance on the “parent” module or does this need to happen in a logic hook? Also - this is on version 8.0.3 so I’m unsure if the logic hook syntax is still the same as the 7.12.5 versions I was testing it on. If it is logic hook - I’d assume it would be an after_save logic hook on the ClientNotes field right? Something along the lines of

$unitsWorked = $bean->units; //this is the value that is calculated by the workflow when a record is saved.. would this get calculated before the logic hook fires, or after??
$services = $bean->get_linked_beans('varDefNameForLink');

$services->CurrentWorkedUnitBalance= CurrentWorkedUnitBalance+ $unitsWorked;


So then the problem becomes… say someone has to change a note from above and Note1 is now 7 units… recalc would add a total of 7 additional units when it should only add 2 for the modified change… My "bean work’ is still a bit on the newb side there… but can I go up and down the relates some how… In pathing it out I figured that the CurrentWorkedUnitBalance would be essentially Sum(units) from the related ClientNotes as a running total - thoughts or suggestions on how to make it work that way?

Side note - there will be a couple other fields that get passed around on this too, but similar concept.

Also - is this type of logic even possible to do from a workflow? I know you can do calculations, or you add/modify a record… but this would be doing a combination of both in one step.

Thank you!

Thank you!!

The solution to this would be logic hooks.

After Save Logic hook in the Units Module . So when a record for this module is modified, the hook will calculate all units and update total in Parent Record.

Was able to get it figured out, a bit more required than that. I had to set it on the Units module, but then load the related parent module, then reload ALL related notes from the parent, do the sums and then update the parent.

Got it to work though!

1 Like