This question has been flagged
8090 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

Avatar
Discard

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

Author

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

Tengo este metodo que uso para una automatizacion:
env.cr.execute('''SELECT max(id) from x_codigos_de_proveedor''')
res = env.cr.fetchone()
record['x_studio_many2one_field_nwIWS'] = res[0]