Odoo Help


How to get the id of last inserted record ?

Nanda Kanoko
on 9/2/13, 12:34 AM 2,977 views

Hello , I need to get the id of recently inserted record automatically so that i can insert the id into a field .

Here's the code :

def onchange_project_sheet(self, cr, uid, ids, project_sheet, context=None):
        res = {}
        project_obj = self.pool.get('notebook.project').search(cr, uid, [('project_id','=', project_sheet)])
        last_insert_id = cr.lastrowid
        for p in self.pool.get('notebook.project').browse(cr, uid, project_obj):
            cr.execute('insert into account_analytic_line (account_id,user_id,unit_amount,amount,product_id,product_uom_id,general_account_id,journal_id,name,date,to_invoice) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) RETURNING id into hr_analytic_timesheet (line_id) where id=%s', (p.account_id,p.user_code,p.unit_amount,p.amount,p.product_id,p.product_uom_id,p.general_account_id,p.journal_id,p.desc,p.date,p.to_invoice))
        return res

This code give me error tuple index out of range

If possible , i want to do it with only 1 cr.execute , with the ids of recent inserted record in account.analytic.line automatically inserted into field line_id in hr.analytic.timesheet or if there's any other option , please enlighten me . Thanks

take the max(id) to take the last entered id

Abhishek H Menon
on 9/9/13, 10:51 AM

and where do i put that inside my code ? please enlighten me thanks

Nanda Kanoko
on 9/11/13, 1:07 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 9/2/13, 12:34 AM
Seen: 2977 times
Last updated: 3/16/15, 8:10 AM