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:
- Generate a javascript object
- 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