Home Blog How To Create a Sequence Number In Odoo
How To Create a Sequence Number In Odoo

How To Create a Sequence Number In Odoo

Odoo ERP has become one of the top choices for businesses because of its ability to handle every possible business need. The little services that Odoo provides help in the growth of the business. Odoo helps in the proper management of your records.

It provides us with an option to create our own sequence for naming the records.

Naming the records in a proper way will help us distinguish similar records from one another just by a look, and by doing so we will be able to neatly manage the storing process of our data. In Odoo, the sale orders and invoices are named using a sequence number.

This helps in identifying a record just by its name. It also provides us with the provision of creating a sequence field for any record that we want.


 Subscribe for Odoo tips, technical insights, and more!


In this blog, we are going to see how we can generate a sequence number for a field.

The sequential field in Odoo, also known as an auto incremental field, generates an auto incremental number every time a new record is generated.

If we need to add a sequence field for naming in our custom model, we can do this easily by making the name field incremental. We can also generate a sequence number by creating a new field or inheriting an old field and making that field auto-incremental.

So, first of all, we need to create a "data folder" in our module in which we will create an "ir.sequence" file, which is an XML file and will help in setting up the sequence we want to create for our field.

Here, I have created a custom module name "sequence_order" in which I will create the basic folders and files ('data, models, views, manifest file, init.py file') required for creating a module in case we are creating a new module.

Also Read:  How To Create or Develop a Custom Module in Odoo

For old modules, simply add the "ir.sequence" file to the data folder(create if not present).

Add Sequence Number in Odoo : Step by Step Process

We can simply add the data file with other ".xml" files in the Views folder but creating a new folder is the proper convention in Odoo and it helps us in distinguishing and identifying the data file.

In the models.py, we have our custom model "sequence.order".

In this custom model, we'll create a new field named 'sequence'. This will be our auto incremental field that we'll be using to generate a sequence.

After this, we'll inherit the default create a method of Odoo and we'll add our custom code to it. This custom code includes creating an environment for the 'ir.sequence' model and calling our custom sequence method here.

------ Code ------

@api.model

    def create(self, vals):
        vals['sequence'] = self.env['ir.sequence'].next_by_code('sequence.order')
        return super(SequenceOrder, self).create(vals)

Add Sequence Number in Odoo : Step by Step Process

Next, we need to create the record which we are referring to in the create method. This record is created in the data file.

<record id="sequence_order.seq_order" model="ir.sequence">
            <field name="name">Sequence Order</field>
            <field name="code">sequence.order</field>
          <field name="number_next_actual">001</field>
            <field name="prefix">Seq Order </field>
            <field name="padding">1</field>
</record>

For the data file, we need to follow these simple steps:

The first thing is setting up the record id and model for the record.

The id is the unique id for reference of this record, and the model remains the same.

Then we set the name of this sequence.

Then the code, the number with which the sequence should start, and then the prefix for this number. We can also set up a suffix for the sequence.

Add Sequence Number in Odoo : Step by Step Process

In the xml file, we can write <data noupdate="1"> , if we want to make sure that the sequence does not get updated when we update the module.

In the view file of our custom module, we can display this sequence field at the top in the header like this:

<div class="oe_title">
<h1>
<field name="sequence" readonly="1"/>
</h1>
</div>

We have used the ‘oe_title’ class of Odoo to define the sequence at the top.

Add Sequence Number in Odoo : Step by Step Process

Next thing, that we need to make sure of is that we call the sequence file from the data folder in the manifest file of Odoo.


Add Sequence Number in Odoo : Step by Step Process

Now, we'll install/upgrade the module and after that, we'll see that our field is being displayed at the top in the form view.

Add Sequence Number in Odoo : Step by Step Process

This field will be uneditable. We'll now enter the required information in the record and then click on Save. After that, we'll see that the record is saved and a sequence is generated. This sequence is auto-incremental for each record.

Add Sequence Number in Odoo : Step by Step Process

We hope this blog is helpful to you and able to explain the entire process of generating or Creating Sequence Numbers in Odoo ERP. 

In case you have any queries related to the process, let us know in the comment section or you get in such with our experts to get best odoo customization services for your business.

Code Download From GitHub

Kanak Infosystems LLP. - Youtube Channel

Get In Touch with Us

Leave a Comment

Your email address will not be published.

Submit
Your comment is under review by our moderation team.