This question has been flagged
1 Reply
2460 Views

I am creating a syncing function where when a Sales order is created/edited it will sync it to our other CRM. I'm inheriting the Sales order module as follows :


@api.model
def create(self, values):
# Override the original create function for the sale_order model
record = super(sale_order, self).create(values)

return record

def write(self, values):
record = super(sale_order, self).write(values)

for record in self:

if record.company_id.x_studio_is_zoho_sales_order_sync == True:


company_id = record.company_id
partner = record.partner_id
partner_id = partner.id
partner_obj = self.env['res.partner'].search([('id','=',partner_id)])

...


However i noticed that when creating/editing a Sales order , the write function is executed several times (almost 20 or more). is there a way i can stop it from executing multiple times ?

Avatar
Discard
Best Answer

Whenever you create any record, there are lots of auto-compute fields or other processes which call the write method frequently.

In your case, you should execute your code in the write method only if company_id is modified. So when you get "values.get('company_id')", then you should execute your code in the write method and don't need to execute it all the time.

def write(self, values):
record = super(sale_order, self).write(values)
if values.get('company_id'):
# If company_id is updated and boolean field is checked in selected company
for rec in self.filtered(lambda x: x.company_id.x_studio_is_zoho_sales_order_sync == True):
# Your code goes here......
return record
Avatar
Discard
Author

After adding the if values.get('company_id'):

The write method is not being triggered anymore at all when creating/editing the sales order. what seems to be the issue with it ?

You did not understand how the write method works and what I explained. Your code will only execute in the write method when company_id is changed.

Instead of company_id, you can use any other field on which you want to execute your code.