Syntax in ECMA rule for "members.findFirst"

Hello,

i have a rule with the code line:

val windowsitem = gWindowsfind.members.findFirst[ id | id.name == windowsname ]

Can somebody help me to find the right code syntax for my ECMA rule. I have no idea where I can find the required information.

Thank you!

First of all, you have to pull the Item from the Item Registry.

var gWindowsfind = ir.getItem("gWindowsfind");

There are two paths you can take.

  1. Convert the Java ArrayList that members returned to a native JavaScript Array and use the built in JavaScript stuff to manipulate it. However, Nashorn is ECMAScript 5.1 so Arrays.find doesn’t exist so you’ll have to implement the find function yourself. Yuck.

  2. Keep it as a Java ArrayList and use the Java native stuff to manipulate it. This requires using the Java Stream API. Luckily this works almost exactly like it does in Rules DSL (I’m pretty sure it’s what is used behind the scenes) but there are a few differences. For example, findFirst doesn’t work the same way (it just returns the first element wrapped in an Optional object).

val windowsitem = gWindowsfind
                    .members
                    .stream()
                    .filter(function(id) { return id.name == windowsname; })
                    .findFirst()
                    .get();

I need to update the Working with Groups in Rules page but have other higher priority stuff to deal with first.

1 Like

Sorry to push that up again, but did you find a way to do this with ECMA? I’m really struggling with that right now and it would be good to have an example :wink:

Edit: Got it working by using the exact syntac from Rich. So i just didn’t understand the post in the first time

2 Likes