{"id":459,"date":"2019-08-16T07:49:14","date_gmt":"2019-08-16T07:49:14","guid":{"rendered":"https:\/\/mellowhost.com\/blog\/?p=459"},"modified":"2019-08-16T07:49:14","modified_gmt":"2019-08-16T07:49:14","slug":"how-to-add-custom-sale-order-line-in-odoo-12","status":"publish","type":"post","link":"https:\/\/mellowhost.com\/blog\/how-to-add-custom-sale-order-line-in-odoo-12.html","title":{"rendered":"How to Add Custom Sale Order Line in Odoo 12"},"content":{"rendered":"\n<p>There are two ways you can add custom sale order line in Odoo 12.<\/p>\n\n\n\n<p><strong>First Method<\/strong><\/p>\n\n\n\n<p>You can search the sale order first with the name or browse with the ID<\/p>\n\n\n\n<p><code>sale_order = self.env['sale.order'].search([('name', '=', 'SO009')])<\/code><\/p>\n\n\n\n<p>Now, you can see the sale order line IDs using the following:<\/p>\n\n\n\n<p><code>sale_order.order_line<\/code><\/p>\n\n\n\n<p>As this is a One2many relation, we need to use a tuple in the Odoo ORM write() method. Here is the list of data you can pass in a tuple to change data that has One2many or Many2many relation<\/p>\n\n\n\n<p><a href=\"https:\/\/www.odoo.com\/documentation\/10.0\/reference\/orm.html#odoo.fields.One2many\">Odoo ORM Documentation<\/a><\/p>\n\n\n\n<p>You can now pass this as python dictionary for &#8216;order_line&#8217; to sale.order of desired product as following:<\/p>\n\n\n\n<p><code>sale_order_new.write({ <\/code><br>        <code>'order_line': [<\/code><br>                   <code>(0,0, {<\/code><br>                                     <code>'order_id': sale_order.id, <\/code><br>                                     <code>'product_id': 2003, <\/code><br>                                     <code>'price_unit': 3000.0, <\/code><br>                                     <code>'product_uom_qty': 2.0, <\/code><br>                                     <code>'name': ''<\/code><br>                                 <code>})<\/code><br>                    <code>] <\/code><br>           <code>})<\/code><\/p>\n\n\n\n<p><strong>Second Method<\/strong><\/p>\n\n\n\n<p>In this method, you can simply define the order dictionary, and create a sale.order.line item with the order_id in reference. Here is an example<\/p>\n\n\n\n<p><code># define dictionary to add order_line<br>order_line_dict = { 'order_id' : 9, 'product_id': 2003, 'price_unit': 3000.0, 'product_uom_qty': 2.0, 'name': '' }<br><br># search the order line for the sale order<br>sale_order_line = self.env['sale.order.line'].browse(9)<br><br># create a new order line<br>sale_order_line.create(order_line_dict)<br><br># print sale order lines<br>sale_order_line<\/code><\/p>\n\n\n\n<p><strong>Note<\/strong><\/p>\n\n\n\n<p>If you are doing\/testing this on the Odoo Shell, then make sure to run the following after the changes to reflect on database:<\/p>\n\n\n\n<p><code>self.env.cr.commit()<\/code><\/p>\n\n\n\n<p>Odoo doesn&#8217;t call database cursor commit if you are coding on Odoo shell, it calls automatically though if you are doing it in a module. Just a friendly reminder!<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are two ways you can add custom sale order line in Odoo 12. First Method You can search the sale order first with the name or browse with the ID sale_order = self.env[&#8216;sale.order&#8217;].search([(&#8216;name&#8217;, &#8216;=&#8217;, &#8216;SO009&#8217;)]) Now, you can see the sale order line IDs using the following: sale_order.order_line As this is a One2many relation, &hellip; <a href=\"https:\/\/mellowhost.com\/blog\/how-to-add-custom-sale-order-line-in-odoo-12.html\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Add Custom Sale Order Line in Odoo 12&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[292,293],"tags":[],"_links":{"self":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/459"}],"collection":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/comments?post=459"}],"version-history":[{"count":1,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/459\/revisions"}],"predecessor-version":[{"id":460,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/459\/revisions\/460"}],"wp:attachment":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/media?parent=459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/categories?post=459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/tags?post=459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}