# Populate relation with random entities

**URL:** <https://community.fibery.io/t/populate-relation-with-random-entities/5205>\
**Category:** Get Help\
**Created:** [September 26, 2023, 9:44pm UTC](https://community.fibery.io/t/populate-relation-with-random-entities/5205 "2023-09-26T21:44:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sarah\_Arminta](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/sarah_arminta/32/4915_2.png) [@Sarah\_Arminta](https://community.fibery.io/u/Sarah_Arminta)\
**Post date:** [September 26, 2023, 9:44pm UTC](https://community.fibery.io/t/populate-relation-with-random-entities/5205/1 "2023-09-26T21:44:29Z")

</div>

How would I write script to populate a relation column with a number of random entities from another database?

Say I have a Day entity auto created each morning. I want to have a column “Baby Lunch” that is automatically filled with 3 random entities from my “Food” database (even better if I can get 3 from the Food database that have a value of True for the “Baby” column).

I’ve been working on figuring it out the last hour and it should be easier than this!

---

<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:** [September 26, 2023, 10:22pm UTC](https://community.fibery.io/t/populate-relation-with-random-entities/5205/2 "2023-09-26T22:22:12Z")

</div>

Assuming you can write a script - my approach would be this:

> - Get all the BabyFood entities
> - Initialize BabyLunch = `[]`
> - Loop 3 times:
> - Choose a random BabyFood entity that’s not already in BabyLunch, and add it to BabyLunch
> 
> - Update `Day.BabyLunch`

---

<div class="post-metadata">

**Author:** ![Sarah\_Arminta](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/sarah_arminta/32/4915_2.png) [@Sarah\_Arminta](https://community.fibery.io/u/Sarah_Arminta)\
**Post date:** [September 27, 2023, 2:08pm UTC](https://community.fibery.io/t/populate-relation-with-random-entities/5205/3 "2023-09-27T14:08:34Z")

</div>

I’ve been trying and I don’t know how to write the script 😅

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [September 28, 2023, 8:18am UTC](https://community.fibery.io/t/populate-relation-with-random-entities/5205/4 "2023-09-28T08:18:53Z")

</div>

Something like this might work:

```auto
const fibery = context.getService('fibery');
// query all Foods
const query = await fibery.graphql('SpaceName', "{findFoods{id}}");
// get the array of food entities
const foods = query['data']['findFoods'];

for (const entity of args.currentEntities) {
    // do three times
    for (let i = 0; i < 3; i++) {
        // generate a random number
        const rand = Math.floor(Math.random() * foods.length);
        // extract a random food
        const food = foods.splice(rand, 1)[0];
        // link the task based on the ID
        await fibery.addCollectionItem(entity.type, entity.id, 'Baby Lunch', food['id']);
    }
}

```

Note: this assumes that the Baby Lunch field is initially empty. If not, then you would need to add a line of code to clear it before adding in the randomly selected ones.

---

<div class="post-metadata">

**Author:** ![Sarah\_Arminta](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/sarah_arminta/32/4915_2.png) [@Sarah\_Arminta](https://community.fibery.io/u/Sarah_Arminta)\
**Post date:** [September 28, 2023, 2:44pm UTC](https://community.fibery.io/t/populate-relation-with-random-entities/5205/5 "2023-09-28T14:44:38Z")

</div>

I changed it slightly to this (with chatGPTs help lol):

```auto
const fibery = context.getService('fibery');
// query all Foods
const query = await fibery.graphql('Testing', "{findFoods{id}}");
// get the array of food entities
const foods = query['data']['findFoods'];

for (const entity of args.currentEntities) {
    // do three times
    for (let i = 0; i < 3; i++) {
        // generate a random number
        const rand = Math.floor(Math.random() * foods.length);
        // extract a random food
        const food = foods.splice(rand, 1)[0];
        // link the task based on the ID
        await fibery.addCollectionItem(entity.type, entity.id, 'Baby Lunch', food['id']);
    }
}

```

and IT’S WORKING!!!

I might actually be able to use Fibery seriously now!! Thank you sooooo much 🙏

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [September 28, 2023, 2:51pm UTC](https://community.fibery.io/t/populate-relation-with-random-entities/5205/6 "2023-09-28T14:51:07Z")

</div>

Yeah, sorry, I should have made clear that you would need to choose the correct SpaceName for your specific case.  
Hope it proves useful.

---

<div class="post-metadata">

**Author:** ![Sarah\_Arminta](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/sarah_arminta/32/4915_2.png) [@Sarah\_Arminta](https://community.fibery.io/u/Sarah_Arminta)\
**Post date:** [September 28, 2023, 3:06pm UTC](https://community.fibery.io/t/populate-relation-with-random-entities/5205/7 "2023-09-28T15:06:10Z")

</div>

Oh actually the part I changed was this:

> [@Sarah\_Arminta](#):
>
> `const food = foods.splice(rand, 1)[0];`

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [September 28, 2023, 3:17pm UTC](https://community.fibery.io/t/populate-relation-with-random-entities/5205/8 "2023-09-28T15:17:02Z")

</div>

Oh dear, yes, i made a typo. I shall correct the original post.
