跳至內容
選單
此問題已被標幟
4 回覆
5125 瀏覽次數

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



頭像
捨棄