Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
11251 Vistas
Hello,I want to create a only one (1) planning activity with an automated action but Odoo creates 6. 

if record.stage_id.id is 2:
env['mail.activity'].create({
'display_name': 'text',
'summary':'text',
'date_deadline':datetime.datetime.now(),
'user_id':record.user_id.id,
'res_id':record.id,
'res_model_id':828,
'activity_type_id':4})
Avatar
Descartar

Too little info.. Where is this code called? Is it in a for loop?

Mejor respuesta

It looks like you are using the env['mail.activity'] model to create a new activity each time the if condition is met. This will create a new activity each time the if condition is met, resulting in multiple activities being created.

To create only one activity, you can use the write() method to update the existing activity instead of creating a new one. You can do this by first checking if the activity already exists using the search() method, and then using the write() method to update the existing activity.

Here is an example of how you can do this:

# check if the activity already exists
activity = env['mail.activity'].search([('res_id', '=', record.id), ('activity_type_id', '=', 4)])

# if the activity exists, update it
if activity:
    activity.write({
        'display_name': 'text',
        'summary': 'text',
        'date_deadline': datetime.datetime.now(),
        'user_id': record.user_id.id,
    })
# if the activity does not exist, create it
else:
    env['mail.activity'].create({
        'display_name': 'text',
        'summary': 'text',
        'date_deadline': datetime.datetime.now(),
        'user_id': record.user_id.id,
        'res_id': record.id,
        'res_model_id': 828,
        'activity_type_id': 4
    })

This code will first check if an activity with the specified res_id and activity_type_id already exists. If it exists, it will update the existing activity with the new values. If it does not exist, it will create a new activity. This will ensure that only one activity is created for each res_id and activity_type_id.

Avatar
Descartar
Mejor respuesta

Hi, you can try this : 

todos = {
'res_id': record.id,
'res_model_id'
: self.env['ir.model'].search([('model', '=', 'your.model')]).id,
'user_id'
: user_id,
'summary'
: 'your task title'),
'note'
: 'your task message'),
'activity_type_id'
: 4,
'date_deadline'
: datetime.date.today(),
}

self.env['mail.activity'].create(todos)

Best regards,

Altela (altelasoftware.com)

Avatar
Descartar