Odoo Help

Welcome!

This community 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.

0

How to get the id of last inserted record ?

By
Nanda Kanoko
on 9/2/13, 12:34 AM 1,925 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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

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