Skip to Content
Menu
This question has been flagged
3 Replies
6796 Views
Hi there,

I have a model event_mgmt that inherits from crm.lead. We would like to create a reminder (mail.activity) as a result of a change in the stage_id.

 @api.model
def _onchange_stage_id_values(self, stage_id):
""" returns the new values when stage_id has changed """
if not stage_id:
return {}
stage = self.env['crm.stage'].browse(stage_id)
if stage.on_change:
return {'probability': stage.probability}
return {}

@api.onchange('stage_id')
def _onchange_stage_id(self):
values = self._onchange_stage_id_values(self.stage_id.id)
event_user_id = self.user_id
self.update(values)
if self.stage_id.id == 3:
self = self.env['event_mgmt.event_mgmt'].browse(self._origin.id)
activity = self.env['mail.activity']
activity_ins = activity.create(
{'res_id': self.id,
'res_model_id': 170,
'res_model':'event_mgmt.event_mgmt',
'activity_type_id':2,
'summary': 'Follow Up',
'note':'Han pasado 5 días desde el paso a propuesta. Realizar seguimiento a la propuesta enviada.',
'date_deadline': date.today() + timedelta(days=5),
'activity_category':'default',
'previous_activity_type_id': False,
'recommended_activity_type_id': False,
'user_id': self.user_id.id
})  

Apparently the mail.activity record is created but I get an error in the console becuse there is an attempt of updating the same event_mgmt but with all its fields but the dates empty, obviously it fails.

The error:

2019-06-13 18:30:34,698 5544 ERROR Yimby_GestionComercialEventos_v0.63 odoo.sql_db: bad query: b'UPDATE "event_mgmt_event_mgmt" SET "venue_id"=NULL,"active"=false,"team_id"=NULL,"partner_id"=NULL,"assembly_finish_time"=NULL,"date_closed"=\'2019-06-13 18:30:34\',"activity"=false,"assembly_start_time"=NULL,"attendants"=0,"timing_notes"=NULL,"general_notes"=NULL,"event_finish_time"=NULL,"user_id"=NULL,"photographer_note"=NULL,"activity_note"=NULL,"email_from"=NULL,"priority"=NULL,"date_last_stage_update"=\'2019-06-13 18:30:34\',"stage_id"=NULL,"event_start_time"=NULL,"dj_note"=NULL,"name"=NULL,"photographer"=false,"probability"=0.0,"write_uid"=1,"write_date"=(now() at time zone \'UTC\') WHERE id IN (2)'
ERROR: el valor null para la columna «name» viola la restricción not null
DETAIL: La fila que falla contiene (2, null, 0, null, null, null, null, null, null, null, 1970, null, null, f, null, f, null, f, null, null, null, null, f, null, null, null, null, null, null, 2019-02-17 22:18:17.975986, 2019-06-13 16:30:34.547321, null, null, f, opportunity, null, 2019-06-13 18:30:34, null, null, null, 2019-02-17 23:18:17, 0, 0, 2019-06-13 18:30:34, null, 0, 0, null, null, 3, null, null, null, null, null, null, null, null, null, null, 1, null, null, null, null, 1, 1, null, null, null, null, null, null, 0, 0, 0, null, null, null).

I don't get why the very same record is updated with null values, Any clue? I'm pretty sure I'm missing something and any help would be very welcome.

Thank you in advance,


Avatar
Discard
Author Best Answer

Thank you for your help Niyas. 

I forgot to mention that the event_mgmt is an already existing record so when it stage is modified, the mail.activity should be created (assuming that my code is not buggy).

What I mean is that the onchange event occurs about an already existing record and after the mail.activity is actually created there is an update with all its parameters null.

* * * * *

In the end I followed the instructions by Odoo Tools and the creation of the activity is done by overriding the write() method of the event_mgmt model.

Thanks!

Avatar
Discard

Dear SCA. Could you provide your code. I am trying to do something similar but keep failing.

Best Answer

it is not a good idea to create anything in an onchange method. This method is for interface views, not for logic. In onchange a record might have not even been created. That might be the reason of the 'null error'.

Instead of that use 'inverse' attribute of the field (or do it in ORM write). E.g. during inheriting:

@api.multi
def _inverse_stage_id(self):
for record in self:
#create activity based on record values
stage_id = fields.Many2one(inverse=_inverse_stage_id)


Avatar
Discard
Best Answer

Hi,

The error message seems to be not in English. It will be nice if you can post the error message in English too.


Coming to the error message, it seems you haven't supplied the value for a field which is required. The name of the field is name itself.


Check the required fields in the model activity and supply values for it during creation.


Thanks

Avatar
Discard
Related Posts Replies Views Activity
0
Sep 24
3
0
Sep 24
347
1
Jun 24
664
1
Apr 24
1703
1
Mar 24
914