Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
4 Trả lời
5126 Lượt xem

Hello, I would like to have a server action to copy a SourceField.value of a Contact to a TargetField.value of another model (MyModel) having a contact field (which must match with the one I launch the server action (which I suppose must be Python code).

Can someone help me? Thank you!!

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

Inside the server action, you can add below code with necessary changes. First we have to search and find the similar record from the other model, for this you can use search orm.


other_model_record = env['model_name'].search([('field_name', '=', self.field_in_current_model)], limit=1)

This will give you record from other model, now using write you can record value into it.


other_model_record.write({'field_name': value})

Thanks

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

You can integrate the following code within the server action, making required adjustments.

class YourServerAction(models.Model):

    _name = 'your.server.action'


    @api.model

    def copy_field_values(self):

        record = self.env['other.model'].search([('contact_field', '=', self.contact_id)], limit=1)

        record.write({'target_field': record.source_field})


Hope it helps

Ảnh đại diện
Huỷ bỏ
Tác giả

Thank you!

Câu trả lời hay nhất

Worked for me odoo 15.

for record in records:

  sale_order = env['sale.order'].search([('name', '=', record.invoice_origin)], limit=1)

  record.update({

    "invoice_date":sale_order.date_order

  })

Ảnh đại diện
Huỷ bỏ
Tác giả

Thank you!

Tác giả Câu trả lời hay nhất

Thank you for your help!

But I tried to adapt your proposal. what I wrote does not create any error, but nothing happens.


Can you see what is wrong in this (the field x_studio_many2one_field_yTsVo.id is the contact field)?

other_model_record = env['res.partner'].search([('id', '=', model.x_studio_many2one_field_yTsVo.id)], limit=1)
model.update ({'x_studio_group' : other_model_record.x_studio_first_name})



Ảnh đại diện
Huỷ bỏ