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.
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 ) ) )
}