# Select additional fields when getting entity

**URL:** <https://community.fibery.io/t/select-additional-fields-when-getting-entity/1033>\
**Category:** Get Help\
**Created:** [October 6, 2020, 8:30am UTC](https://community.fibery.io/t/select-additional-fields-when-getting-entity/1033 "2020-10-06T08:30:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haslien](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/haslien/32/493_2.png) [@Haslien](https://community.fibery.io/u/Haslien)\
**Post date:** [October 6, 2020, 8:30am UTC](https://community.fibery.io/t/select-additional-fields-when-getting-entity/1033/1 "2020-10-06T08:30:13Z")

</div>

Anyone know if it’s possible to also select additional data from related entities with the internal button API?

I want to do the [same thing as here](https://community.fibery.io/t/get-all-entities-in-relation/736), but this time around also select to include fields of the returned children, like workflow state.

Else I would have to resort to using the HTTP API.

```auto
const field3Collection = {
    "q/select": [{
        "App/Field 3": [
            "q/select": [
                {"workflow/state": ["enum/name"]}
            ],
            "q/limit": 1
        ]
    }],
    "q/limit": "q/no-limit"
};

fibery.getEntityById("App/Name", entity["My entity"], ["Field 1", "Field 2", field3Collection]);

```

---

<div class="post-metadata">

**Author:** ![Haslien](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/haslien/32/493_2.png) [@Haslien](https://community.fibery.io/u/Haslien)\
**Post date:** [October 6, 2020, 12:04pm UTC](https://community.fibery.io/t/select-additional-fields-when-getting-entity/1033/2 "2020-10-06T12:04:35Z")

</div>

For now I’ll use HTTP request and API.

> <https://github.com/KeyboardRage/Fibery-scripts/blob/main/button/how_many_children_of_parent_have_state_completed.js>

---

<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:** [October 6, 2020, 2:08pm UTC](https://community.fibery.io/t/select-additional-fields-when-getting-entity/1033/3 "2020-10-06T14:08:50Z")

</div>

I was looking at the original post you referred to and was playing around with something similar. I thought I would upload some sample code that shows how to get properties (incl collections) of related types using the action button.  
In this case, I have a Project with multiple related Tasks. When the button is pressed (for any Task) the code finds the parent Project and then finds all Tasks associated with this Project and iterates through renaming them.  
If I’ve understood your problem, it would seem to be possible to adapt the code to get other properties of the parent entity as you need.

```
const api = context.getService("fibery");

for (const e of args.currentEntities) {

    const task = await api.getEntityById(e.type, e.id, ["Project"])
    const projectID = task["Project"].id

    const project = await api.getEntityById("Project", projectID, ["Name","Tasks"])
    const projName = project["Name"]

    const taskCollection = project["Tasks"].map(u => u.id)

    var i = 0
    for (const task of taskCollection) {
        i = i + 1
        const taskObj = await api.getEntityById("Task", task, ["Property"])
        const newName = projName + " " + i + " - " + taskObj["Property"]
        await api.updateEntity("Task", task, { "Name": newName })
    }
}

```

FYI, I’m not a coder, so I’m sure it could be done more nicely :-/

---

<div class="post-metadata">

**Author:** ![Haslien](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/haslien/32/493_2.png) [@Haslien](https://community.fibery.io/u/Haslien)\
**Post date:** [October 7, 2020, 7:41am UTC](https://community.fibery.io/t/select-additional-fields-when-getting-entity/1033/4 "2020-10-07T07:41:11Z")

</div>

Using `getEntityById` unfortunately only gets the ID of collections (_like workflow state_). In that case I would then have to at least get each uniquely appearing state ID to read its data that I can use for further logic.

Not sure how fast that would be compared to a single request that is able to get all data. Depending on how the backend logic works and other conditions, either could be faster and more efficient than the other 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![Sergey\_Truhtanov](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/sergey_truhtanov/32/13986_2.png) [@Sergey\_Truhtanov](https://community.fibery.io/u/Sergey_Truhtanov)\
**Post date:** [October 7, 2020, 8:06am UTC](https://community.fibery.io/t/select-additional-fields-when-getting-entity/1033/5 "2020-10-07T08:06:59Z")

</div>

By the way for the case above (load nested entities with fields), we introduced `getEntitiesByIds` method. So you first load entities of level 1, and then load entities of level 2 with required fields, performing only 1 query. So this still be good in terms of efficiency.  
But definitely commands api will be still faster, as only one query is required. But as commands api is a bit cubersome, we introduced this simplified shortcuts methods in button api.

---

<div class="post-metadata">

**Author:** ![Haslien](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/haslien/32/493_2.png) [@Haslien](https://community.fibery.io/u/Haslien)\
**Post date:** [October 7, 2020, 9:08am UTC](https://community.fibery.io/t/select-additional-fields-when-getting-entity/1033/7 "2020-10-07T09:08:25Z")

</div>

@Chr1sG Using what Sergey suggested using method to get multiple entities all at once (_I think he meant at least_):

`const tasks = await api.getEntityById("Project", projectID, ["Name","Tasks"]);`

Returns something like

```auto
[{
	name:"Task 1",
	state: {
		Id: "aaaaaa-bbbbbbbb"
	}
},{
	name:"Task 2",
	state: {
		Id: "bbbbbb-aaaaaaaaa"
	}
}]

```

which is not enough info for `state`, so I would have group them uniquely:

```auto
const stateIds = new Set(tasks.map(t => t.state.Id));

```

then require all those tasks with a second query

```auto
const states = fibery.getEntitiesByIds("Task", Array.from(stateIds), ["enum/name"]);

```

to then proceed with logic. This works too of course. As Sergey said, it is simpler to create, less of a mess, but a single query is faster.
