0
votes

I'm trying to create a custom activity using 'mail.activity' model, with custom values, and show me this error:

File "/usr/lib/python3/dist-packages/odoo/addons/mail/models/mail_activity.py", line 108, in 
_compute_res_name
activity.res_name = self.env[activity.res_model].browse(activity.res_id).name_get()[0][1]
IndexError: list index out of range

I think is because in the core mail.activity model, the access to the 0 and 1 position in the array crash my program, but I dont know why.

Here is my code for the activity creation:

@api.model
def create(self, vals)
    user_id = self.user_id
    date_deadline = datetime.now() + timedelta(days=7)

    data = {
        'res_id': self.id,
        'res_model_id': self.env['ir.model'].search([('model', '=', 'hr.applicant')]).id,
        'user_id': user_id.id,
        'summary': 'foo bar',
        'activity_type_id': self.env.ref('custom.activity_applicant').id,
        'date_deadline': date_deadline
        }
    self.env['mail.activity'].create(data)
    return super().create(vals)

There is something in the field res_id that I don't know about or something else.

2
Where are you creating this record, check the self.id what is its value exactlyCharif DZ
Make sure that this function is called from he.applicant model only so self.id will be applicant's id.Purvi Bhatt

2 Answers

1
votes

I already figured out what was wrong, I was calling self.id before even creating the object, and id is a null value then. I change the object creation before this part of the code and it works fine.

@api.model
def create(self, vals)
    new = super().create(vals)
    user_id = new.user_id
    date_deadline = datetime.now() + timedelta(days=7)

    data = {
        'res_id': new.id,
        'res_model_id': self.env['ir.model'].search([('model', '=', 'hr.applicant')]).id,
        'user_id': user_id.id,
        'summary': 'foo bar',
        'activity_type_id': self.env.ref('custom.activity_applicant').id,
        'date_deadline': date_deadline
        }
    self.env['mail.activity'].create(data)
    return new
0
votes

You got that error because Odoo expects a list of pairs (id, text_repr) for each record and it found probably an empty list (the returned result will be used to build dictionaries directly from sequences of key-value pairs to prepare the display names).

Make sure the returned result by the hr applicant's name_get method is a list of pairs.

@api.multi
def name_get(self):
    result = []
    res = super(HrApplicant, self).name_get()
    # the result list should be filled with pairs
    return result