# Error while returning result to field.Function(field.Many2many) exist in a one2many relation table

**URL:** <https://discuss.tryton.org/t/error-while-returning-result-to-field-function-field-many2many-exist-in-a-one2many-relation-table/4314>\
**Category:** User\
**Created:** [June 13, 2021, 11:31am UTC](https://discuss.tryton.org/t/error-while-returning-result-to-field-function-field-many2many-exist-in-a-one2many-relation-table/4314 "2021-06-13T11:31:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aso](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/a/f04885/32.png) [@aso](https://discuss.tryton.org/u/aso)\
**Post date:** [June 13, 2021, 11:31am UTC](https://discuss.tryton.org/t/error-while-returning-result-to-field-function-field-many2many-exist-in-a-one2many-relation-table/4314/1 "2021-06-13T11:31:38Z")

</div>

i have a field exist in one2many relation table this field i choose it to be Many2Many field type with coding a function make me display the content of another field exist in another class in line2 on the table one2many : this is how i coded the function inside the class refer to the table one2many relation:  
`class "work.test" refer to the one2many relation table`

````
class Test(DeactivableMixin, ModelSQL, ModelView, MultiValueMixin):
    __name__ = 'work.test' ```
    field1 = fields.Function(
            fields.Many2Many('work.work', 'test', None, "Status"), 'get_state')
    @classmethod
    def get_state(cls,institutions, Name):
        pool = Pool()
        parametr_up = pool.get('process.operation')
        res=[]
        operation1 = Transaction().context.get('operation1')
        institution = 0
        for institution in institutions:
            institutions = parametr_up.search([
                    ('institution', '=', institution.id),
                    ], limit=1)
            if institutions and parametr_up.operation1 :
              if institution = 2:
                res[institution.id] = institutions[0].operation1.convention
            else:
                res[institution.id] = None
        institution = +1        
        return res 
```

````

What does NOT work:  
the error :

```
Traceback (most recent call last):
  File "/trytond/wsgi.py", line 117, in dispatch_request
    return endpoint(request, **request.view_args)
  File "/trytond/protocols/dispatcher.py", line 47, in rpc
    return methods.get(request.rpc_method, _dispatch)(
  File "/trytond/wsgi.py", line 84, in auth_required
    return wrapped(*args, **kwargs)
  File "/trytond/protocols/wrappers.py", line 156, in wrapper
    return func(request, pool, *args, **kwargs)
  File "/trytond/protocols/dispatcher.py", line 181, in _dispatch
    result = rpc.result(meth(*c_args, **c_kwargs))
  File "/trytond/model/modelsql.py", line 872, in read
    row[fname] = getter_result[row['id']]
IndexError: list index out of range

```

---

<div class="post-metadata">

**Author:** ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)\
**Post date:** [June 13, 2021, 10:20pm UTC](https://discuss.tryton.org/t/error-while-returning-result-to-field-function-field-many2many-exist-in-a-one2many-relation-table/4314/2 "2021-06-13T22:20:19Z")

</div>

The result of the `getter` function for a `Function` field of type `Many2Many` must be a list of ids.  
But also you must ensure to provide a vale for all the records the `getter` is called for. In your example `institutions` is replaced in the loop.

---

<div class="post-metadata">

**Author:** ![aso](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/a/f04885/32.png) [@aso](https://discuss.tryton.org/u/aso)\
**Post date:** [June 14, 2021, 8:03am UTC](https://discuss.tryton.org/t/error-while-returning-result-to-field-function-field-many2many-exist-in-a-one2many-relation-table/4314/3 "2021-06-14T08:03:20Z")

</div>

Hi , that you for your reply but am sorry i didn’t get you clearly , How i can correct it! or is their any example in one of the standard module can help me to reach my goal !

---

<div class="post-metadata">

**Author:** ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)\
**Post date:** [June 14, 2021, 8:50am UTC](https://discuss.tryton.org/t/error-while-returning-result-to-field-function-field-many2many-exist-in-a-one2many-relation-table/4314/4 "2021-06-14T08:50:10Z")

</div>

The problem of your code is that it does not return a value for all of the records that are passed as argument. So this makes tryton crash. You should fix your function to return the proper type for all records.
