This question has been flagged
3 Replies
11019 Views

I'm pretty new to Odoo and Python in general and am trying to add a mod to our helpdesk.  We built a wizard to set up a form to add an IT tech to multiple tickets at once.  I know I should use context.get('active_ids') so I can go through all the selected tickets, but I'm having issues getting it to work.  Any advice would be appreciated

helpdesks_ids = fields.Many2many('helpdesk.ticket')
user_ids = fields.Many2many('res.users', string="IT Technicians", required=True)

@api.multi
def action_assign_tech(self, vals):
      t = self.env['helpdesk.ticket'].browse(self._context.get('active_ids'))
      for ticket in self.helpdesks_ids:
           t.user_id = ticket.user_id

Avatar
Discard
Best Answer

First off, "self" is the record generated by the wizard. So when you want to access fields from the wizard use: self.my_field

Second, you should use a Many2one field for user_id, because you can only assign one user to the field helpdesk.ticket.user_id (it's a Many2one field).

You don't have to add the field helpdesks_ids, because you are getting these records using browse on active_ids.

When writing the same values to multiple records, use the write() method instead of a loop. It's faster.

Try this:

user_id = fields.Many2one('res.users', string="IT Technicians", required=True)

@api.multi
def action_assign_tech(self):
    tickets = self.env['helpdesk.ticket'].browse(self._context.get('active_ids'))
    tickets.write({'user_id': self.user_id.id})
Avatar
Discard
Best Answer

Hi,

Update your code like this,


@api.multi
def action_assign_tech(self, vals):
      tickets = self.env['helpdesk.ticket'].browse(self._context.get('active_ids'))
      for tic in tickets:
#adjust the below line as per the need
#by tic you can access each tickets
           tic.user_id = tic.user_id

Thanks

Avatar
Discard
Author Best Answer

@Marius Stedjan

Very much appreciate your answer! Thanks a bunch!

Just so you know, when I tested it, I first got error "Unable to set NOT NULL on 'user_id'" to which I fixed by getting rid of "required=True".

Then I got error "Can't adapt type 'res.users'", to which I fixed by changing

tickets.write({'user_id': self.user_id})# previously

tickets.write({'user_id': self.user_id.id})# revised

Thanks so much for putting me on the right path!

Avatar
Discard

You’re welcome!

Sorry about forgetting the .id. It was too late at night, hehe. I’ve updated the answer.

As for the required=True;

I suppose you got this when you didn’t set a user in the wizard? The point of required is to force the user to set a user.. which makes sense in a wizard like this.