you can try this way:
from odoo import models, fields, api
class SaleOrder(models.Model):
_inherit = "sale.order"
workflow_process_id = fields.Many2one(
comodel_name="sale.workflow.process",
string="Automatic Workflow",
ondelete="restrict",
)
@api.model
def create(self, vals):
# Check if company_id is present in vals
if 'company_id' in vals:
company_id = vals['company_id']
default_workflow = self.env['sale.workflow.process'].search([
('company_id', '=', company_id),
], limit=1)
if default_workflow:
vals['workflow_process_id'] = default_workflow.id
return super(SaleOrder, self).create(vals)
- We override the create method of the sale.order model.
- We check if the company_id is present in the values being passed to the method.
- We search for a sale.workflow.process record with a matching company_id.
- If a matching record is found, we set the workflow_process_id field to the ID of that record in the vals dictionary.
- Finally, we call the original create method to actually create the sale order.
Make sure to replace 'sale.workflow.process' with the actual model name of the workflow process, and adjust any other field names or model names as needed based on your actual Odoo configuration.