# Unable to filter records based on grand parent collection

**URL:** <https://community.forestadmin.com/t/unable-to-filter-records-based-on-grand-parent-collection/4304>\
**Category:** Help me!\
**Created:** [March 9, 2022, 6:40am UTC](https://community.forestadmin.com/t/unable-to-filter-records-based-on-grand-parent-collection/4304 "2022-03-09T06:40:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saurav\_Suman](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/saurav_suman/32/3709_2.png) [@Saurav\_Suman](https://community.forestadmin.com/u/Saurav_Suman)\
**Post date:** [March 9, 2022, 6:40am UTC](https://community.forestadmin.com/t/unable-to-filter-records-based-on-grand-parent-collection/4304/1 "2022-03-09T06:40:53Z")

</div>

## Feature(s) impacted

> Advanced Filter / Search records in a collection

## Observed behavior

> Let’s assume we have 3 tables as below with these column names -

- Organization - org\_id(PK), org\_name
- Department - dept\_id(PK), dept\_name, org\_id(FK)
- Employee - emp\_id(PK), emp\_name, dept\_id(FK)  
Currently when we try to filter a Employee collection based on org\_name, we don’t get option to select organization in subfield of Department.

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/8/89d85b0f496554e8fd16332ca47cf99ca9f7accb.png)

## Expected behavior

> We should get option to select organization in subfield of department when filtering employees.

## Context

> Please provide in this **mandatory section** , the relevant information about your configuration:

- Project name: Curator
- Team name: Developer
- Environment name: ALL (Staging, Development, Production)
- Agent type & version:
- Package Version: 8.3 (forest-express-sequelize)
- Express Version: 4.17.1
- Sequelize Version: 6.6.5
- Database Dialect: MySql
- Database Version: 5.7.24

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [March 9, 2022, 4:50pm UTC](https://community.forestadmin.com/t/unable-to-filter-records-based-on-grand-parent-collection/4304/2 "2022-03-09T16:50:17Z")

</div>

Hello @Saurav_Suman

Nested filtering is actually not supported by forest admin’s agents which are deployed on your architecture.

The only way to achieve something similar to what you want is to create a smartfield, and implement the filter method.

We’re actually working on an upgrade for the agent package which, among many things, will bring nested filtering to forest admin

It should release in a couple months.

---

<div class="post-metadata">

**Author:** ![Saurav\_Suman](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/saurav_suman/32/3709_2.png) [@Saurav\_Suman](https://community.forestadmin.com/u/Saurav_Suman)\
**Post date:** [March 15, 2022, 11:16am UTC](https://community.forestadmin.com/t/unable-to-filter-records-based-on-grand-parent-collection/4304/3 "2022-03-15T11:16:09Z")

</div>

Hey,  
Thanks @anon39940173  
I was able to implement smart field to support nested filtering for some collections.  
But I am facing issue implementing the global search for the smart field ?

Could you please let me know if its possible to apply search on the smart fields that is fetched from grand collection ?

Below is sample code snippet for what I am trying to achieve but its not working saying “No Employees Match your search”

```javascript

...
search: async function (query, /** @type {any} */ search) {
    const orgs = await models.Org.findAll({
      where: { name: { [Op.like]: `%${search}%` } },
    });
    const _where = { [Op.or]: orgs.map(
        (/** @type {{ id: any; }} */ orgs) => {
          return { orgId: orgs.id };
        }
      )};
    const depts = await models.Dept.findAll({
      where: _where,
    });
    const emps = await models.Emp.findAll({
      where: {
        [Op.or]: depts.map(
            (/** @type {{ id: any; }} */ dept) => {
              return { deptId: dept.id };
            }
          )
        },
    });

    const empConditions = emps.map(
      (/** @type {{ id: any; }} */ emp) => {
        return { id: emp.id };
      }
    );
    query.where[Op.and][0][Op.or].push({
      [Op.or]: empConditions,
    });
    return query;
  },
  ...

```
