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

create sale.order (master-detail) using web services API - cannot insert order line

By
Andy Utomo
on 6/14/16, 9:34 AM 674 views


I try to create sales order using web services API, 

I do this : 

product2 = objects.execute_kw(db,uid,pwd, 'product.template','search',[[['id','=',11]]])

product2_ids = objects.execute_kw(db,uid,pwd,'product.template','read',[product2],{'fields':

['name',

'type',

'default_code',

'barcode']})

customer = objects.execute_kw(db,uid,pwd, 'res.partner','search',[[['customer','=',1],['id','=',31]]])

customer_ids = objects.execute_kw(db,uid,pwd,'res.partner','read',[customer],{'fields':

['name',

'street']})

print 'customer id :', customer_ids[0].get('id')

print 'Product id :', product2_ids[0].get('id')

param = objects.execute_kw(db, uid, pwd,'sale.order', 'create',[

{'partner_id' :customer_ids[0].get('id'),

'validity_date':datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'),

'order_line': {'product_id':product2_ids[0].get('id'),'name':'test','product_uom_qty':10,'price_unit':30000}}])

salesorder = objects.execute_kw(db,uid,pwd, 'sale.order','search',[[['id','=',param]]])

print objects.execute_kw(db,uid,pwd,'sale.order','read',[salesorder],{'fields':

['name',

'id',

'order_line']})


Then when I See the result,

customer id : 31

Product id : 11

[{'id': 31, 'name': 'SO16060031', 'order_line': []}]

Process finished with exit code 0


the object successfully Create Sales Order, but cannot insert the order line..

So how to fix it ? is there any sample or documentation about insert sale.order  and sale.order.line or similar


Best Regards




0
Andy Utomo
On 6/14/16, 10:38 AM

Solve,

 my mistake .. I should create sale.order first, get the return value, then create sale.order.line with the return value as "order_id:


param =  objects.execute_kw(db, uid, pwd,'sale.order', 'create',[
{'partner_id' :customer_ids[0].get('id'),
'validity_date':datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'),
'order_line': {'product_id':product2_ids[0].get('id'),'name':'test','product_uom_qty':10,'price_unit':30000}}])
objects.execute_kw(db, uid, pwd,'sale.order.line', 'create',[
{'order_id' :param,'product_id':product_ids[0].get('id'),
'name':'test2','product_uom':product_ids[0].get('uom_id')[0],'product_uom_qty':20,'price_unit':50000,'price_total':1000000}])
objects.execute_kw(db, uid, pwd,'sale.order.line', 'create',[
{'order_id' :param,'product_id':product2_ids[0].get('id'),
'name':'test45','product_uom':product2_ids[0].get('uom_id')[0],'product_uom_qty':10,'price_unit':50000,'price_total':5000000}])
salesorder = objects.execute_kw(db,uid,pwd, 'sale.order','search',[[['id','=',param]]])
print objects.execute_kw(db,uid,pwd,'sale.order','read',[salesorder],{'fields':
['name',
'id',
'order_line']})
0

The value given for the one2many field order_line should be a list of commands.  Use the command (0, 0, vals) to create a new line:


line_vals = {
'product_id': product2_ids[0].get('id'),
'name':'test',
'product_uom_qty': 10,
'price_unit': 30000,
}
order_vals = {
'partner_id': customer_ids[0].get('id'),
'validity_date': datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'),
'order_line': [(0, 0, line_vals)],
}
salesorder = objects.execute_kw(db, uid, pwd, 'sale.order', 'create', [order_vals])

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: 6/14/16, 9:34 AM
Seen: 674 times
Last updated: 7/5/16, 8:36 AM