Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
4 ตอบกลับ
5103 มุมมอง

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!!

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

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

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

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

อวตาร
ละทิ้ง
ผู้เขียน

Thank you!

คำตอบที่ดีที่สุด

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

  })

อวตาร
ละทิ้ง
ผู้เขียน

Thank you!

ผู้เขียน คำตอบที่ดีที่สุด

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})



อวตาร
ละทิ้ง