Skip to Content
Menu
This question has been flagged
2 Replies
10836 Views

hello i'm actually encountering a problem, I have followed the tutorial as shown in this link \https://www.youtube.com/watch?v=qyRhjyp1MeE and there is no error in code, but when i'm creating a new employee it doesn't generate a reference number. Reference number remains 'New' in tree view and form view for the created employee. Can anyone help me please?


hr.py :

    name_seq = fields.Char(string='Order Reference', required=True, copy=False, readonly=True, index=True, default=lambda self: _('New'))

    @api.model

    def create(self, vals):

        if vals.get('name_seq', _('New')) == _('New'):

            vals['name_seq'] = self.env['ir.sequence'].next_by_code('hr.employee.sequence', sequence_date=seq_date) or _('New')

        result = super(HrEmployeePrivate, self).create(vals)

        return result

        

sequence.xml:


<?xml version="1.0" encoding="utf-8"?>

<odoo>

    <data noupdate="1">


        <!-- Sequences for transfer.order -->

        <record id="seq_hr_employee" model="ir.sequence">

            <field name="name">Employee Sequence</field>

            <field name="code">hr.employee.sequence</field>

            <field name="prefix">EMP</field>

            <field name="padding">3</field>

            <field name="company_id" eval="False"/>

        </record>


    </data>

</odoo>


hr_views.xml :


                        <div class="oe_title">

                            <h1>

                                <field name="name_seq" readonly ="1"/>

                            </h1>

                            <h2>

                                <field name="name" placeholder="Employee's Name" required="True"/>

                            </h2>

                            <h3>

                                <field name="job_title" placeholder="Job Position" />

                            </h3>

                            <field name="category_ids" widget="many2many_tags" options="{'color_field': 'color', 'no_create_edit': True}" placeholder="Tags"  groups="hr.group_hr_manager"/>

\\\Answer\\\
Avatar
Discard
Best Answer

In python file add a field in your model like the below code.


name = fields.Char(string="Service Number", readonly=True, required=True, copy=False, default='New')


Then add a function like in the following code.


@api.model
def create(self, vals):
   if vals.get('name', 'New') == 'New':
       vals['name'] = self.env['ir.sequence'].next_by_code(
           'self.service') or 'New'
   result = super(SelfService, self).create(vals)
   return result


After defining function,  you need to create a record for this. So create a record in the XML file .


<record id="sequence_self_service" model="ir.sequence">
   <field name="name">Self Service</field>
   <field name="code">self.service</field>
   <field name="active">TRUE</field>
   <field name="prefix">SS</field>
   <field name="padding">6</field>
   <field name="number_next">1</field>
   <field name="number_increment">1</field>
</record>


By doing this, a sequence number gets automatically generated, on creating a record in this model. 


> name- Name of the record created in the “ir.sequence” model

> code- Sequence code 

> active- A boolean field to indicate whether it is active or not

> prefix- A character field, where we can give the prefix of the sequence

> padding- sequence size

> number_next- next number that will be used

> number_increment- the next number of the sequence will be incremented by this.

 

In the below image -we can see a sequence number is generated for the record with prefix SS and number  000001(odoo will automatically add ‘0’s to the left side of the next number to get the required padding size-here we given padding size =6, so the first sequence number becomes 000001).


how-to-add-sequence-in-odoo12-cybrosys


We can configure the sequence number from the settings of odoo.


Go to Settings -> Technical -> Sequences & Identifiers -> Sequences


The below image shows the corresponding record of sequence number created using the above code.


how-to-add-sequence-in-odoo12-cybrosys


There are two types of the implementation method 

 1. No gap

 2. standard


No gap defines that there will be no gap in the numbering of records.


For example, if a self-service form is created, it will be numbered as SS000001. Then the second will be SS000002. If I deleted the second form and create a third one, then in case of 


> No gap- sequence  will be SS000002

> Standard- sequence will be SS000003


In the prefix and suffix field, you can add a prefix and suffix of the sequence. You can define the date range for sequence. For this, you have to check the “Use subsequences per date_range” field. 


For example, if you want to start sequence number from 1 at the start of every month, then define the date range in the from and to the field and next number as 1.


how-to-add-sequence-in-odoo12-cybrosys



Avatar
Discard

Really great explanation Imran. I have been able to make this work but when I create a new record from a One2many tree view, the sequence number does not generate. Any idea how I could get it to work in such a case?

Best Answer

Hello. Did you find a solution to the problem that it remains as "new"?

Avatar
Discard

Please make sure that the sequence is created in the db with same sequence code that you are calling to get sequence number