Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged
2 Odgovori
1857 Prikazi
Hello everyone,

I am currently seeking assistance with setting up a cron job in Odoo v16 on odoo.sh. 
Specifically, I want to create a script that adds an activity to all 'opportunities' in the CRM module that are not 'won', provided there is no existing activity.

Here is the script I have written so far:

leads = env['crm.lead'].search([('activity_ids', '=', False), ('probability', '

for i in range(0, len(leads), 10):
    batch_leads = leads[i:i+10]
    
    for lead in batch_leads:
        activity = env['mail.activity'].create({
            'res_id': lead.id,
            'res_model_id': env.ref('crm.model_crm_lead').id,
            'activity_type_id': 13,
            'date_deadline': datetime.datetime.today().strftime('%Y-%m-%d %H:%M'),
            'user_id': lead.user_id.id,
        })
        lead.write({'activity_ids': [(4, activity.id)]})

However, this script does not seem to work as expected. If anyone could provide guidance or suggestions to correct this script, I would greatly appreciate it.

Thank you in advance for your help and support.

Best regards

Avatar
Opusti
Avtor

Full script:
leads = env['crm.lead'].search([('activity_ids', '=', False), ('probability', '<', 100)])

for i in range(0, len(leads), 10):
batch_leads = leads[i:i+10]

for lead in batch_leads:
activity = env['mail.activity'].create({
'res_id': lead.id,
'res_model_id': env.ref('crm.model_crm_lead').id,
'activity_type_id': 13,
'date_deadline': datetime.datetime.today().strftime('%Y-%m-%d %H:%M'),
'user_id': lead.user_id.id,
})
lead.write({'activity_ids': [(4, activity.id)]})

Avtor Best Answer

working script - missing was the last line: env.cr.commit()

leads = env['crm.lead'].search([('activity_ids', '=', False), ('probability', '


for i in range(0, len(leads), 10): 

batch_leads = leads[i:i+10]

for lead in batch_leads:

activity = env['mail.activity'].create({

'res_id': lead.id,

'res_model_id': env.ref('crm.model_crm_lead').id,

'activity_type_id': 30,

'date_deadline': datetime.datetime.today().strftime('%Y-%m-%d %H:%M'),

'user_id': lead.user_id.id,

})

lead.write({'activity_ids': [(4, activity.id)]})


env.cr.commit()

Avatar
Opusti
Best Answer

Hi,

Please update your code like this and try, it was verified working,

leads = env['crm.lead'].search([('activity_ids', '=', False), ('probability', ' 




for i in range(0, len(leads), 10):

​ batch_leads = leads[i:i+10]

​ for lead in batch_leads:

​ activity = env['mail.activity'].create({

​ 'res_id': lead.id,

​ 'res_model_id': env.ref('crm.model_crm_lead').id,

​ 'activity_type_id': 13,

​ 'date_deadline': datetime.datetime.today().strftime('%Y-%m-%d %H:%M'),

​ 'user_id': lead.user_id.id,

​ })

​ lead.write({'activity_ids': [(4, activity.id)]})



Thanks

Avatar
Opusti
Related Posts Odgovori Prikazi Aktivnost
2
avg. 25
788
2
maj 25
1650
1
apr. 25
1616
0
mar. 25
1641
0
mar. 25
1576