Skip to Content
Menu
This question has been flagged

I have created a Customer and that customers has 3 contact person names. Now while creating the Sales Order, I want to select 1 contact person name from a list. How can I get only the names of the contact person into a field?

I have created a related field in my custom module and in the form view I have displayed it :

custom.py

customer_contact = fields.Char(related="partner_id.child_ids.name", string="Contact Person", )

custom_saleorder_views.xml

<xpath expr="//field[@name='partner_shipping_id']" position="after">
<!-- Add your fields or attributes here -->
<field name="customer_contact" />
</xpath>

I have displayed the field below the Delivery address.

Now when I select the customer in Sales Order form view, it gives only the first contact person name. The rest of the contact names are not displayed.

How to get a list of contact names in the field "customer_contact". As the default field type of 'name' field is 'Char', using many2one field in our custom module gives a error.

Avatar
Discard
Best Answer

Hi,

you can not used related="partner_id.child_ids.name" because  child_ids is One2many field

One2many fields does not work in related. 

Please try below Example:

customer_contact = fields.Char(string="Contact Person" )

@api.onchange('partner_id')

def get_customer_contact_person(self):

    if self.partner_id and self.partner_id.child_ids:

        for person in self.partner_id.child_ids:

            self.customer_contact =  ' '.join(person.name)

Make sure it will helpful.

Thank You.


Avatar
Discard
Author

Hi Dhaval,

I tried you code, but it game me the following error. Can you please help me in solving this error.

line 66, in get_customer_contact_person

self.customer_contact = ''.join(person.name)

TypeError: can only join an iterable

Hello Arpit,

you can try below:

def get_customer_contact_person(self):

if self.partner_id and self.partner_id.child_ids:

self.customer_contact = ' '.join(str(person.name) for person in self.partner_id.child_ids)

Author

Hi Dhaval,

Your updated solution is working well without any problem. Thank you for your prompt answer.

With your code I am getting contact person names, but not as a drop down list. I want to select only one contact person from 3 or 4 persons.

So can you help me in getting a drop down menu for contact persons.

Thanks for your previous answers.

Related Posts Replies Views Activity
3
Sep 18
2341
2
Mar 15
8022
2
Jul 24
6653
1
Oct 22
2456
2
Jun 20
5109