Update a text field with a calculated value using the Scripts tab when a page is edited/saved/published

Hi Sebastian,

I have a bit of an edge case.

In my product schema I have a text field called lowestPrice. In my app the user will be able to sort by this field in ASC/DESC order. Algolia requires you to have one index per sort operation. So in my case I need one index sorted by price in ASC and another index sorted by price in DESC. In the indexes you specify the lowesPrice field. This algolia property will be mapped to the lowestPrice field in Squidex.

I also have another field in my product schema that is called retailers. This is a reference field. The schema of this ref field is called retailer. The retailer schema has a price field.

So what I need to do is loop through all the pages of type retailer and look for the lowest price. When the lowest price is found in the loop I then need to assign this value to the lowestPrice field. Can this be done in the Scripts tab in my product schema?

If this is not at all possible what would you recommend I do instead?

Yes and no. You have two updates:

  1. Whenever a retailer price is changed you have to update all products.
  2. Whenever a product is changed you might have to update the product by querying all retailers.

Theoretically there are script functions to do that, but I don’t recommend it. I would just solve that in a webhook to be honest.

Thanks for the reply and for the webhook solution tip. I think I will stick with manually updating the lowestPrice field for now. I do not think it is that much of an overhead since there will not be that many products. If it becomes a pain in the future i will then consider implementing a webhook.

1 Like