'Odoo - Context defined in view not passed in form

I'm using Odoov15, I try to inherit the Odoo sale_crm module. To make my tests, I modify the sale_order_views.xml in sale_crm/views.

I add thoses lines in the sale_view_inherit123 form :

<xpath expr="//field[@name='payment_term_id']" position="after">
     <field name="opportunity_id" help="Log in the chatter from which opportunity the order originates" context="{'default_type': 'opportunity', 'default_phone': 'XX XX XX XX XX'}"/>
</xpath>

The code is working as the field is indeed added after the payment_term_id field. However, the "Create and Edit" button of the field does not hold any of the default data, the type is still lead at creation and no default phone is given.

In debug mode, I can clearly see the context of my field is "Context: {}", so it's like the field is added, but the context is removed in the form.

My goal is to have the opportunity created from the sale_order view with opportunity as a type instead of lead when I create it via the many2one field in the sale_order form (with create and edit or just create). The "Phone" in the line of code above was just for the test. I'll probably prefill a lot more fields but it would be a good start.



Solution 1:[1]

This is because there is a default set on the field level. You are using the default feature that will convert all opportunities in the lead as a step for verification, check this default code

type = fields.Selection([('lead', 'Lead'), ('opportunity', 'Opportunity')], index=True, required=True, tracking=15,default=lambda self: 'lead' if self.env['res.users'].has_group('crm.group_use_lead') else 'opportunity', help="Type is used to separate Leads and Opportunities")

To make it work, you will need to override this default.

Debug in compute to check if you have context value there and if yes, the change values accordingly

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1