1
votes

I am trying to loop through my One2Many records to avoid duplication.

class sales_target(models.Model):
    _name = 'sales.target'
    _description = 'Sales Target'

    name = fields.Char(string='Name',required=True)
    from_date = fields.Date(string='From Date',required=True)
    to_date = fields.Date(string='To Date',required=True)
    sales_team = fields.Many2one('crm.team',required=True)
    sales_record_ids = fields.One2many('sales.target.record','sales_target_rec_id',string='Sales Record')  

    @api.one   
    def check_duplication(self,result):   
        count = 0
        if self.sales_record_ids:    
            for record in self.sales_record_ids:
                if result.id == record.sales_person_p_id:                  
                    count = 1
        if count == 0:  
            self.write({'sales_record_ids':[(0,0,{'sales_person':result.name})]})     

    @api.one    
    def get_sales_person(self): 
        for res in self.sales_team.member_ids:            
            self.check_duplication(res)         

The other class is as:

class sales_target_record(models.Model):
    _name = 'sales.target.record'

    sales_target_rec_id = fields.Many2one("sales.target")    
    sales_person = fields.Char(string='Sales Person',readonly=True,required=True)
    sales_person_p_id = fields.Char(compute='get_value',store=True)



 @api.onchange('sales_person')
    @api.depends('sales_person')
    def get_value(self):
        res = self.env['res.partner'].search([('name','=',self.sales_person)])          
        self.sales_person_p_id = res[0].id

Now when I am hitting the button i still have duplicate records. However I tried to compare with name and things work good but I cannot compare with names since its not correct because names can be same but id cannot. That function was as:

 @api.one   
 def check_duplication(self,result):   
     count = 0
     if self.sales_record_ids:            
         for record in self.sales_record_ids:
             if result.name == record.sales_person:                    
                 count = 1
     if count == 0:         
         self.write({'sales_record_ids':[(0,0,{'sales_person':result.name})]})    

Hope for guidance on this.

2

2 Answers

0
votes

Can you try like this

@api.multi   
def check_duplication(self,result):   
    if self.sales_record_ids:            
       for record in self.sales_record_ids:
           if not result.name == record.sales_person:                           
              self.write({'sales_record_ids':[(0,0,{'sales_person':result.name})]})    
0
votes

Concluding from the fact that for name it works properly, something might be wrong with your if condition.

sales_person_p_id is of type char, however you seem to compare it with an integer: result.id.

Have you made sure that both objects in your if condition are of the same type?

Try to make sales_person_p_id an integer field (e.g. via sales_person_p_id = fields.Integer(compute='get_value',store=True) or do some kind of type casting before comparing the objects.