Generating an user list by subtracting other users lists

I need help with a formula for a button:

I have 4 relation to users field in my meetings: Expected, Present, Informed absent and Non-informed absent.

For the expected I already have a button that generate the list of expected people. Presents I usually add manually, so as Informed absent. Now I wanted to create a Generate Non-Informed Absent List that would take the Expected users list, subtract the presents and the informed absents and get to a result of Non-informed absent.

For example, users A, B, C, D, E, F and G are expected. Presents are A, B and D. C has informed his absence. So when I click my button, it would automatically fill my non-informed absence field with E and F.

Does that make sense? Is it possible?

This should work.
You may need to adjust the collection names I’ve used to match your DB.

// Find Users from 'Expected' collection that are not in 'Present' or 'Informed Absent' collections,
// and add these Users to the 'Non-Informed Absent' target collection.
// NOTE: we assume the target collection is empty to begin with (we do not first empty it).

const fibery = context.getService('fibery')
const targetField = 'Non-Informed Absent'       // Name of target collection to populate

for (const ent0 of args.currentEntities) {
    // Get source collections
    const entity = await fibery.getEntityById( ent0.Type, ent0.Id, ['Expected', 'Present', 'Informed Absent'] )
    const found_in = (e, alist) => !!entity[alist].find(x => x.Id === e.Id)
    // Build the list of Users in 'Expected' that are not in 'Present' or 'Informed Absent'
    const results = entity['Expected'].filter( e => !found_in(e, 'Present') && !found_in(e, 'Informed Absent'))
    // Add the resulting Users to the target collection
    await Promise.all( results.map( user => fibery.addCollectionItem( ent0.Type, ent0.Id, targetField, user.Id ) ) )
}
1 Like

Should I add this formula here?

I did like that but then it points several bad characters… I’m I doing it right?

Thank you so much for helping me with this =)

In your “Then” section instead of “Update” switch that to “Script” and then you can put the script in the field that show.

@gomocoop The Button Action needs to be “Script” - then you will see the correct place to paste in the script text.

1 Like

YEY!

It worked perfectly!

Thank you!