Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
4 Antwoorden
5124 Weergaven

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

Avatar
Annuleer
Beste antwoord

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

Avatar
Annuleer
Beste antwoord

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

Avatar
Annuleer
Auteur

Thank you!

Beste antwoord

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

  })

Avatar
Annuleer
Auteur

Thank you!

Auteur Beste antwoord

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



Avatar
Annuleer