Skip to Content
Menú
This question has been flagged
2 Respostes
3633 Vistes

i have multiple companies setup.

i have sale form there is a field called workflow_process_id, 

i want this field having different default value based on which company_id

e.g. if company_id = 1 open the sale form, this workflow_id is 'workflow a'

how should i retrieve the company_id and modify this routine?


class SaleOrder(models.Model):    

​_inherit = "sale.order"
    ​workflow_process_id = fields.Many2one( 

  comodel_name="sale.workflow.process",        string="Automatic Workflow",

        ondelete="restrict",    )

Avatar
Descartar
Best Answer

Hii,

You can add option in company form to set the default workflow for each companies.

Add a Many2one Field in the Company Model:




and define the field in company form view also

after this modify the default_get function of sale.order model as below:




With these changes, users can select the default workflow for each company through the company form, and that selected workflow will be used as the default value for the workflow_process_id field when creating a sale order.


Hope this will help you
Thanks

Avatar
Descartar
Best Answer

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)


  1. We override the create method of the sale.order model.
  2. We check if the company_id is present in the values being passed to the method.
  3. We search for a sale.workflow.process record with a matching company_id.
  4. If a matching record is found, we set the workflow_process_id field to the ID of that record in the vals dictionary.
  5. 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.

Avatar
Descartar
Related Posts Respostes Vistes Activitat
1
de des. 22
2014
3
de nov. 22
4163
1
de nov. 22
4476
1
de maig 22
3807
0
de febr. 22
2247