Atleast Extract Ids for Reference Fields via Scripting to push in Algolia

@Sebastian

Need a little help on the scripting syntax to push referenced field ids while writing payload to send data in Algolia using Webhooks(Rules).

Parent/Child Usecase (One-Many Relationship):

Category Schema (i.e Parent) references Subcategory Schema (i.e Child)

On Trigger of Content updation in Category Entity I want to take the Action to push the payload as defined below related subcategory ids (NOT THE CONTENT OF SUBCATEGORY, BUT JUST THE IDs)

Script(JSON.stringify(
     {
    "cateeorytitle": `${event.data.title['zh-TW']}`
    "subcategory_id": `${event.data.subcategory['zh-TW']}`
     }
    ))

The above dosen’t even push the array of Ids of children subcategory.

:zipper_mouth_face::zipper_mouth_face: This is where I need your help :zipper_mouth_face::zipper_mouth_face:
MAY BE I NEED TO USE A BETTER SYNTAX FOR ACCESSING THE CHILD ARRAY IDS

subcategory_id":${event.data.subcategory[‘zh-TW’]}`

:thinking::thinking::thinking::thinking:PFA the screenshots :thinking::thinking::thinking:

I have highlighted in green the Parent Trigger leads to Child Ids being pushed in algolia via scripting

:sob::sob:Algolia Result:sob::sob:

:zipper_mouth_face::zipper_mouth_face: Squidex Action Payload Definition (Scripting Code):zipper_mouth_face::zipper_mouth_face:

:smiley::smiley:I checked using the GraphQL API Playground the Ids are stored in the Content:smiley::smiley:

Too much emojis …

If you know that you have always exactly one id, you can try:

Script(JSON.stringify(
     {
    "cateeorytitle": `${event.data.title['zh-TW'][0]}`
    "subcategory_id": ${event.data.subcategory['zh-TW'][0]}`
     }
    ))

@Sebastian, apologies for the emojis

Super cool, I managed to pull this off for more than 1 reference ids, now just thinking how can I do it on demand dynamically

Script(JSON.stringify(
     {
    "subcategory_id": 
         {
         "sc1": `${event.data.subcategory['zh-TW'][0]}`,
         "sc2":`${event.data.subcategory['zh-TW'][1]}`,
         "sc3": `${event.data.subcategory['zh-TW'][2]}`
       }, 
      }
    ))

What happens if you just try?

Script(JSON.stringify(
{
    "cateeorytitle": event.data.title['zh-TW']
}
    ))

@Sebastian I just copy pasted the above script for a new rule I created for your experiment

Script(JSON.stringify(
{
    "cateeorytitle": event.data.title['zh-TW']
}
    ))

Result in Algolia

Sorry, I mean your subcategory field ofc.

@Sebastian

Script(JSON.stringify(
{
    "subcategory": event.data.subcategory['zh-TW'][0]
}
    )) 

Here is the record that get’s pushed in Algolia

And here are the Logs in Squidex after the Webhook successfully triggers

{
  "Responses": [
    {
      "ObjectIDs": [
        "a2e8bd62-b702-4cd7-bf0d-6a4f429d9029"
      ],
      "TaskID": 573498394002
    }
  ]
}

Elapsed 00:00:01.0850000.

@Sebastian I didn’t get the purpose of this experiment, are there any interesting takeways that I can include in my Webhooks to improve the experience. Is it that I can do away without the ${...} notation and just use a clear syntax?

The script has two parts:

  1. Generate a javascript object
  2. Serialize it to a string

The first part needs the interpolation sometimes, but it depends on your objects.

For example:

Lets assume that we already have a string as value, then

Script(JSON.stringify(
{
    "field": event.data.field.iv
})) 

is enough.

if we have a boolean and need a string we need to convert it and can use interpolation for that:

Script(JSON.stringify(
{
    "field": `boolean_${event.data.field.iv}`
})) 

In your example the value is an array of strings, which could work fine as well:

So you can try this:

Script(JSON.stringify(
{
    "subcategory": event.data.subcategory['zh-TW']
}))

EDIT: I have tested it with a unit test and it definitely works as expected.

1 Like