Best Practice for Using Functions to Calculate Property Values in Foundry

I have two objects that are linked to each other in a one to many relationship. Ex.

ONE: Author
MANY: Books

Currently, I am writing the Create, Edit, and Delete functions for the Books Object. However, I am stumped on determining the best practices.

I want to “recalculate” my Author: author.totalBooksSold and author.totalBooksInStock properties anytime a Book is Created, Edited, or Deleted. So far, I have written my Create Book Function and it only recalculates one author object at a time.

I want my Total Cost and Average Cost of Books for every Author in my object set to recalculate instead of the just Author that I created a new book for, I’m not sure if I should implement it the way I mentioned in the previous sentence or implement it where only the Author that received new books “recalculates”.

Open to any suggestions.

My code and object schema are pictured in the attached screenshots.



I tried to write my function the way a derived property is written but wasn’t sure how to apply my calculations for every object. See Attached.

Hi Andre, you’re on the right track. Here are my suggestions:

  • If each book only has one author, then I would update each author’s properties whenever you modify a book. I would NOT update all authors’ properties whenever a single book is modified.
  • If a book can have multiple authors, then I would map whatever update functions you have over the array of authors.
  • In Workshop, ensure that the module settings permit live updates to propagate to the relevant object sets – the authors, in this case

There’s an alternate method you might consider, though if you’re short on time save this for a future project. This alternative is to use Automate to run an action whenever a Book is created/updated/deleted. This is essentially a webhook, but for Actions. You can configure this in the Book config page in Ontology Manager. The Action(s) you would call would be the function-backed actions that update author info. The benefit of this approach is that it’s easier to extend in the future. The downside is that you have more actions/functions. I also don’t have direct experience with latency for this approach. I would assume the Automate-triggered actions execute more or less immediately, but I can’t say for sure.

Thanks Taylor. Yes a book only has one author in this example. Do you know if there is a way to create a one time bulk calculate action. In a real world example I can have hundreds of authors and thousands of books. I would like to calculate the totals one time to set the correct values, then after everytime a book is edited or created the recalculate will run only for the corresponding author.


Also, I wrote my create an edit this way and the edit works but the create does not include the newly created object in the calculations. Do you know how I can get around this?

If it’s a one-time thing then your options are:

  1. Take a step back and calculate the initial values in the data pipeine, and then reindex the objects
  2. Write a script that hits the Apply Action API endpoint for each book/author combo. I have no idea what rate limiting will be for this, but if you have ~hundreds or low ~thousands then this should work.

Regarding the issue of your code not including the newly created object in the calculations, you’re running into this limitation. In this situation, I would suggest modifying your performAuthorCalcs function so it accepts the newest book you created, and then append that to the books list variable.

1 Like

Actually, @andre there’s another way of doing this that’s probably easier for your situation: manually run the Actions using Automate. More details in the docs here: Documentation | Automate > Run effects over a set of existing objects