This question has been flagged
6 Replies
8381 Views

Hii,

I have made some fields as required through xpath in xml on CRM Pipeline form. I dont want to set required in python.
When I change stage this validation dont apply. I tried on stage_id onchange function but crm.lead fields are not available.
I tried overriding write method, in this case when creating new lead in Kanban quick create dont work.

How to validate required fields on CRM stage change?

Thanks,

 


Avatar
Discard

Please refrain from obviously boosting your own karma with your "fake" Q&A games, especially when your answers are questionable.

Author

twanda AG, Ermin Trevisan Switzerland, very disgraceful remarks. I am helping me and the community and for you its answer and question game?

Author Best Answer

I done it by overriding the write() method. Here is the code.

class AbcCrmLead(models.Model):     
    _inherit = 'crm.lead' 
@api.multi
def write(self, values):
result = super(AbcCrmLead, self).write(values)
for record in self:
if not record.partner_id:
raise UserError(_('Customer is required'))
if not record.mobile:
raise UserError(_('Mobile is required'))
if record.stage_id.need_quotation and record.sale_number <= 0:
raise UserError(_('Opportunity with No Quotation can\'t be moved to %s stage. Please add quotation(s) to move to next stage.' % record.stage_id.name))
return result

The issue was when creating new opportunity from Kanban Quick Create Form. So I inherited the Kanban Quick Create Form and added required fields. 

<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="abc_quick_create_opportunity_form" model="ir.ui.view">
<field name="name">abc.crm.lead.form.quick_create</field>
<field name="model">crm.lead</field>
<field name="inherit_id" ref="crm.quick_create_opportunity_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='partner_id']" position="attributes">
<attribute name="required">1</attribute>
</xpath>
<xpath expr="//field[@name='partner_id']" position="after">
<field name="mobile" required="1"/>
<field name="date_deadline" required="1"/>
<field name="user_id" required="1"/>
<field name="team_id" required="1"/>
</xpath>
<xpath expr="//field[@name='planned_revenue']" position="attributes">
<attribute name="invisible">1</attribute>
</xpath>
<xpath expr="//field[@name='priority']" position="attributes">
<attribute name="invisible">1</attribute>
</xpath>
</field>
</record>
</odoo>

Now all validation is working by drag and drop from kanabn view, changing stage from form view.

Avatar
Discard
Best Answer

You can use attrs to make the fields required based on condition.

Create a related name field of the stage_id and use it in the attrs.

stage_name = fields.Char(related='stage_id.name', string='Stage') # Create this field in the py file

<field name="stage_name" invisible="1"/>
<field name="field1" attrs="{'required': [('stage_name', '=', 'Draft')]}"/>


Avatar
Discard
Author

Thanks Arya,

<xpath expr="//field[@name='partner_id']" position="attributes">

<attribute name="attrs">{'required': [('stage_id.name', '=', 'New')]}</attribute>

</xpath>

Getting following error...

Error: Unknown field stage_id.name in domain

You need to create a related field of the stage name:

stage_name = fields.Char(related="stage_id.name", string='Stage Name')

<field name="stage_name" invisible="1"/>

<field name="field1" attrs="{'required': [('stage_name', '=', 'Draft')]}"/>

Author

I added the stage_name related field updated required domain as you said. But in following two cases validation dont apply.

1. Drag and Drop from kanban to next stage validation dont apply.

2. On the opportunity form, if I click on the next stage, opportunity stage changes to next and validation dont apply

3. On the opportunity form, if I edit and click on next stage, validation applied, on save also applied

I think solution id somewhere in the python code by overriding write method?

What you say Arya.

Ok in that case, you would need to override the write method, check the stage and the fields values in the vals dict, and raise exception as per your condition.

Author

Thanks Arya, your support was helpful.