Search code examples
pythonodoo-13

On record creation, run a function to create a record in another module - odoo 13


I was trying to create a new company when a record is created in my module using the create method, but I can't get it to work:

    @api.model
    def create(self, vals):
        for n_record in self:
            company_info ={
                'company_type': 'company',
                'name': n_record.new_company_name,
                'vat': n_record.company_ vat
            }
            record = n_record.env['base.view_partner_form'].create(company_info)

            return record

I got this error and I can't understand it:


Odoo Server Error
Traceback (most recent call last):
........
........
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 339, in checked_call
    result = self.endpoint(*a, **kw)
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 915, in __call__
    return self.method(*args, **kw)
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 515, in response_wrap
    response = f(*args, **kw)
  File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 1322, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 1314, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "/usr/lib/python3/dist-packages/odoo/api.py", line 385, in call_kw
    result = _call_kw_model_create(method, model, args, kwargs)
  File "/usr/lib/python3/dist-packages/odoo/api.py", line 366, in _call_kw_model_create
    return result.id if isinstance(args[0], Mapping) else result.ids
AttributeError: 'NoneType' object has no attribute 'id'

I need help with understanding the error.

Edit

I got past the error by changing my code to:

   @api.model
   def create(self, vals):
        company_info ={
            'company_type': 'company',
            'name': self.new_company_name,
            'vat': self.company_vat
        }
        record = self.env['res.partner'].create(company_info)

        return record

I am getting this error now though:

Something went wrong! Contacts require a name

I'm not sure but I think I'm passing the name 'name': self.new_company_name,

Edit 2

(Thank you @NiyasRaphy)

Changed 'name': self.new_company_name, to 'name': vals['new_company_name']

The company is being created but the form is stuck and not being saved.


Solution

  • Thanks to @NiyasRaphy and @Prakash at odoo forms

        @api.model
        def create(self, vals):
            company_info ={
                'company_type': 'company',
                'name': vals['new_company_name'],
                'vat': vals['company_vat'],
            }
            self.env['res.partner'].create(company_info)
    
            result = super(my_class_name, self).create(vals)
            return result