コンテンツへスキップ
メニュー
この質問にフラグが付けられました
4 返信
5117 ビュー

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



アバター
破棄