I’m in the middle of moving from Jython → JS scripting.
I have many rules relying on “Descendants of groupname received update” trigger in Jython.
The reason for that is the group model i’m using with two levels nested groups for different services.
Is there a way to create the same in JS scripting?
There is no rule trigger that works with descendants of a Group. The Jython helper library kind of cheated and invented such a trigger by unrolling the descendants and creating a separate trigger for each individually. All the other languages use the built in triggers and there isn’t a trigger that supports descendants.
To get the same, assuming you are using .js files and not the UI, you can dynamically create the rule triggers the same way that Jython’s helper library did it behind the scenes. As with the Jython implementation, you’ll have to reload the file every time you change the Group membership.
I only use the UI so I’m just taking a stab at it but I think it would look something like this:
// Create functions like this, maybe put them in a personal library for reuse
const generateItemStateChangedTriggers = (groupName) => {
return items[groupName].descendents.map( item => triggers.ItemStateChangedTrigger(item.name) ]
}
rules.JSRule( {
name: "My rule name",
description: "My rule description",
triggers: generateItemStateChangedTriggers('MyGroup'),
execute: (event) =>
...
Or you can just do it inline since it’s just a one liner call to map.
If you are working with the UI, you’ll need to use a GenericEventTrigger configured to match all Item changes (for example) and then use a rule condition to ignore those except from Items that are descendants from the Group you care about.
The disadvantage here is the rule will be constantly be triggering but the advantage is you won’t have to reload the rule if you change the Group membership.
Edit: I was helping someone with map in Rules DSL and used that syntax instead of JS syntax. Doh!