# Generating an user list by subtracting other users lists

**URL:** <https://community.fibery.io/t/generating-an-user-list-by-subtracting-other-users-lists/4503>\
**Category:** Get Help\
**Created:** [May 24, 2023, 10:14pm UTC](https://community.fibery.io/t/generating-an-user-list-by-subtracting-other-users-lists/4503 "2023-05-24T22:14:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gomocoop](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/gomocoop/32/6349_2.png) [@gomocoop](https://community.fibery.io/u/gomocoop)\
**Post date:** [May 24, 2023, 10:14pm UTC](https://community.fibery.io/t/generating-an-user-list-by-subtracting-other-users-lists/4503/1 "2023-05-24T22:14:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Matt\_Blais](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/matt_blais/32/1464_2.png) [@Matt\_Blais](https://community.fibery.io/u/Matt_Blais)\
**Post date:** [May 24, 2023, 11:58pm UTC](https://community.fibery.io/t/generating-an-user-list-by-subtracting-other-users-lists/4503/2 "2023-05-24T23:58:58Z")

</div>

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

```auto
// 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 ) ) )
}

```

---

<div class="post-metadata">

**Author:** ![gomocoop](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/gomocoop/32/6349_2.png) [@gomocoop](https://community.fibery.io/u/gomocoop)\
**Post date:** [May 25, 2023, 1:35pm UTC](https://community.fibery.io/t/generating-an-user-list-by-subtracting-other-users-lists/4503/3 "2023-05-25T13:35:07Z")

</div>

Should I add this formula here?

 ![Captura de Tela 2023-05-25 às 10.32.38](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/0/016284a4342546c45cffe70d7045cf1a44821dff.jpeg)

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

 ![Captura de Tela 2023-05-25 às 10.34.12](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/5/56f715bed22b34c84574e87c7d5e7bae2d08aba8.png)

Thank you so much for helping me with this =)

---

<div class="post-metadata">

**Author:** ![matth](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/matth/32/6392_2.png) [@matth](https://community.fibery.io/u/matth)\
**Post date:** [May 25, 2023, 1:42pm UTC](https://community.fibery.io/t/generating-an-user-list-by-subtracting-other-users-lists/4503/4 "2023-05-25T13:42:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Matt\_Blais](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/matt_blais/32/1464_2.png) [@Matt\_Blais](https://community.fibery.io/u/Matt_Blais)\
**Post date:** [May 25, 2023, 3:39pm UTC](https://community.fibery.io/t/generating-an-user-list-by-subtracting-other-users-lists/4503/5 "2023-05-25T15:39:59Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/0/02dfddf768d315814d87793951a085354e86461a.png)

---

<div class="post-metadata">

**Author:** ![gomocoop](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/gomocoop/32/6349_2.png) [@gomocoop](https://community.fibery.io/u/gomocoop)\
**Post date:** [May 25, 2023, 7:31pm UTC](https://community.fibery.io/t/generating-an-user-list-by-subtracting-other-users-lists/4503/6 "2023-05-25T19:31:17Z")

</div>

YEY!

It worked perfectly!

Thank you!
