Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
2 Odpovědi
3645 Zobrazení

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
Zrušit
Nejlepší odpověď

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
Zrušit
Nejlepší odpověď

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
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
1
pro 22
2024
3
lis 22
4172
1
lis 22
4480
1
kvě 22
3808
0
úno 22
2255