How to update child page when parent page changes? (Referenced schemas)

Hi Sebastian,

I have two schemas:

  • brand
  • product

Schema brand has one field of type String called name

Schema product has one field of type String called name and another one of type Reference called brand

I have created an Algolia Rule for the product schema that gets triggered on the following events:

event.type == 'Published' || event.type == 'Updated' || event.type == 'Unpublished' || event.type == 'Deleted'

Now, whenever I publish, update, unpublish or delete a page of the schema type product the Algolia Rule fires and updates my Algolia index accordingly. So, so far so good.

This is how the record looks like in my Algolia index:

{
   "objectID": "65f9ce73-21d9-4732-a7fb-53cab5f28c65",
   "name": "Product One",
   "brand": "Brand A"
}

Now, what I’m wondering is:

If I go to a page of type brand and update the name field and I change it from let’s says Brand A to Brand A International, how can I trigger an update for all the pages of type product that have a reference to the page of type brand that just got updated?

This is how the record would look like after an update:

{
   "objectID": "65f9ce73-21d9-4732-a7fb-53cab5f28c65",
   "name": "Product One",
   "brand": "Brand A International"
}

By the way, I’m only indexing pages of type product and not pages of type brand.

You have to create the rule for brand instead, then you can use the “Referenced schemas” feature:

Thank you, Sebastian.

I don’t have that feature in my version of Squidex (5.5.0).

It looks like it was introduced fairly recently:

I’ll see how I will handle this problem because if I upgrade I need to take into consideration all the breaking changes including the removal of Orleans and I believe there was something else that I forgot about.

Then you need a custom webhook endpoint where you can solve that.

Yes, and I don’t think i want to go that route if squidex has that built in.

What are the best resources for upgrading squidex? CHANGELOG.md and what else? I know this is off topic but still :slight_smile:

Yes, the changelog mentions long running migrations, that run automatically, but cause a temporary downtime. If you have a single instance there is not that much to do. Just upgrade the version in docker engine or kubernetes.

Hi Sebastian,

I’m not sure I understand how this feature works.

  1. Schemas - is this field always mandatory?
  2. Reference schemas - can I use this field without specifying Schemas?
  3. In Schemas, I can only specify the parent (brand) in this field?
  4. In Schemas, can I specify the child (product) in this field?
  5. Do I need to create a new childProductsOfThisParentBrand Reference field in my brand schema and add all products to that field in order for my case (see below) to work?

In my case the following is true:

  • A brand does not reference any products.
  • A product references one and only a single brand.

So I guess the broader question is - does Rules support bidirectional relationships (parent->child AND child->parent) or only parent->child?

Also, I’m not quite sure how to write my Liquid document? Because the event template variable contains different things depending on if a brand or a product was Updated.

I’ve added the following rule:

Schemas:
brand When event.type == 'Updated'

Referenced schemas:
product When event.type == 'Updated'

but it appears to do nothing when I update a product.

If I update a brand the rule works.

Any ideas?

Update: The Simulator says “STOP: Event does not match to the trigger.”. I don’t know what it means. I thought that would help troubleshoot.There is more stuff in Simulator. Please let me know if you need more info.

I think you should make it the opposite way:

The main trigger is product.

But when any referenced brand is changed also trigger the rule.

Update: The Simulator says “STOP: Event does not match to the trigger.”. I don’t know what it means. I thought that would help troubleshoot.There is more stuff in Simulator. Please let me know if you need more info.

The simulator replays all last events. And if the even (e.g. asseet event) does not match to the trigger it shows this message).

Hi Sebastian,

I’ve setup a super simple minimalistic example.

Here’s how my setup looks like:

brand schema:
image

brand page:

product schema:
image

product page:

Rule:

If I make a change in the product page an event is triggered but when I make a change in the brand page an event is not triggered:

Am I writing the brand condition incorrectly?

The filter on product is wrong. it must event.type === 'Updated' || event.type === 'ReferenceUpdated' (or just remove it)

hmm I noticed you’re using === instead of ==.

In the Rule dialog example == is used.

Is there any difference?

It is just javascript. I am so used to the === notation :wink:

1 Like

Sebastian, I noticed that when I toggle between Text, Script, and Liquid tabs in the Rule UI the text that I write in Liquid tab is automatically copied over to Text and Script tabs. It’s probably the other way around as well. I don’t think it’s supposed to be like that? And if I haven’t written anything in a tab it should be empty?

It is not a tab, it is a mode.

Squidex supports 3 modes for formatting, that have historic reasons.

We just started with simple interpolation, then javascript and then liquid.

The way it works is to add a prefix and suffix to each value, like

Script(const x = 'I am Javascript');

And the “tabs” just do that automatically and use the correct syntax highlighting. If you have suggestions how to make it more clear, it is very welcome. Perhaps just a label or so?

Okay. Hmm, let me think about it for a bit.

1 Like

The Rule is working now btw.

For reference and if someone else finds this useful:

Schemas:

product:

event.type == 'Published' || event.type == 'Updated' || event.type == 'Unpublished' || event.type == 'Deleted' || event.type == 'ReferenceUpdated'

Referenced schemas:

brand:

event.type == 'Updated'

I wrote the Document using Liquid.

Thanks for the help, Sebastian.

People will probably find this post if they run into a similar issue otherwise it would be good to add some info about Referenced schemas and the ReferenceUpdated event in the docs.

1 Like

Hi Sebastian,

I have 3 schemas:

  • product
  • variant-folder
  • variant-color

product has a References field for variant-folder.

variant-folder has a References field for variant-color.

What I would like is to update the product in Algolia whenever the variant-color updates.

Is this possible?

I tried experimenting with different work arounds, by creating a new rule that in turn would trigger another rule etc., but couldn’t make it to work.

Unfortunately not, the references work only at one level. You need custom code for that in a webhook.