Skip to Content
Menu
This question has been flagged
1 Reply
1274 Views

Hello, I have records copied from a create function, I want the number field to come out as a sequence and the attached code is not copied, currently it copies the / in all the records of the number field. 

I would appreciate it if you could help me, thank you 


@api.model    defcreate(self, values):          
        result= super(Visit, self).create(values)      

        values['number'] = self.env['ir.sequence'].next_by_code('visit.visit') or'/'
        if not result._context.get('from_copy') andlen(result.contacto_tag)>1:                        forrecordinresult.contacto_tag:                        result.with_context({'from_copy':True}).copy({'contacto_tag':[(4,record.id)]})                   return result 





Avatar
Discard
Best Answer

you can make the following modifications to your code:

@api.model
def create(self, values):
if 'number' not in values:
values['number'] = self.env['ir.sequence'].next_by_code('visit.visit') or '/'
result = super(Visit, self).create(values)

if not result._context.get('from_copy') and len(result.contacto_tag)>1:
for record in result.contacto_tag:
result.with_context({'from_copy': True}).copy({'contacto_tag': [(4, record.id)]})
return result

Explanation of Changes:

  1. Check if 'number' field exists in the incoming values: Before generating a sequence number, it is necessary to check if the 'number' field is already present in the values dictionary. If it is not present, then generate a new sequence number.

  2. Iterate over records: In the for loop, correct the variable name from record to record (plural form) to iterate over the result.contacto_tag records.

  3. Proper indentation: Ensure that the code inside the for loop is indented correctly for proper execution.

With these modifications, the number field will be set as a sequence number if it is not provided in the values dictionary. Additionally, the "/" character will not be assigned to the number field during the copying of records.

Avatar
Discard