okay so, I am trying to inherit the sale.order.line model and tapping into the create() method. The goal is that when a new order line gets added if it's a service-type product, we add a new order line with a specific product that is already set up on Settings. However, on the create() call, it throws an error saying: Record does not exist or has been deleted. (Record: sale.order.line(115,), User: 2) , I am not sure why. The first thing is, that the record exits and the second thing is, not using it on the create call. Here is my code:
from odoo import models, fields, api from odoo.exceptions import UserError class SaleOrderLine(models.Model): _inherit = "sale.order.line" @api.model def create(self, vals): sale_order_line = super().create(vals) order_id = sale_order_line.order_id.id product_type = sale_order_line.product_template_id.detailed_type shop_supplies_product = self.env["res.config.settings"].get_values()[ "shop_supplies_product" ] shop_supplies_product = self.env["product.template"].browse( shop_supplies_product ) shop_supplies_percentage = self.env["res.config.settings"].get_values()[ "shop_supplies_percentage" ] if ( product_type == "service" and sale_order_line.product_template_id.id != shop_supplies_product and shop_supplies_product ): current_shop_supplies = self.env["sale.order.line"].search( [ ("order_id", "=", sale_order_line.order_id.id), ("product_template_id", "=", shop_supplies_product.id), ], limit=1, ) shop_supplies_amount_nt = 0 if current_shop_supplies: shop_supplies_amount_nt = current_shop_supplies.price_unit current_shop_supplies.unlink() current_shop_supplies_amount_nt = float(sale_order_line.price_subtotal) shop_supplies_amount_nt += ( current_shop_supplies_amount_nt * float(shop_supplies_percentage) ) / 100 order_line_fields = { "customer_lead": 0.0, "name": shop_supplies_product.name, "order_id": order_id, "price_unit": shop_supplies_amount_nt, "product_uom_qty": 1.0, "product_template_id": shop_supplies_product.id, "product_id": shop_supplies_product.product_variant_id.id, } self.env["sale.order.line"].sudo().create(order_line_fields) return sale_order_line