In my custom module,
I have a model crm.question through which I am creating question for every checklist from crm.audit.checklists.
now I have another model crm.lead.question.answer using which I am saving pulling questions and saving answers per record and all checklist with associated questions is populating in crm_lead_view.xml using button.
now In checklist_name field of crm.lead.stages.checklist.items.files model there are some checklist which are selected by some other process,
task: I want to show populate only those checklist item in crm_lead_view.xml view which are present in checklist_name field as per record.
for ex:
if in crm.question if checklist_item_id has value KYC with question_text as "Is KYC done?",
if checklist_item_id has value Doc with question_text as "is all doc submitted?".
1. according to current code flow:
then in answer model: checklist_item_id and question_text will show both 2 checklist item with associated question.
2. expected code flow:
and in checklist_name KYC is present,
then in answer model: only KYC will show checklist_item_id with associated question_text.
code are attached below.
questions.py:
class AuditQuestion(models.Model):
_name = 'crm.question'
_description = 'Audit Questions'
_rec_name = 'question_text' # Set Question Text as the default display name
checklist_item_id = fields.Many2one('crm.audit.checklists', string="Checklist Item", required=True)
question_text = fields.Char(string="Question", required=True)
lead_id = fields.Many2one('crm.lead', string="Audit")
def name_get(self):
"""Customize the display name for crm.question."""
result = []
for record in self:
name = record.checklist_item_id.name if record.checklist_item_id else record.question_text
result.append((record.id, name))
return result
class LeadQuestionAnswer(models.Model):
_name = 'crm.lead.question.answer'
_description = 'Lead-Specific Questions and Answers'
lead_id = fields.Many2one('crm.lead', string="Lead", ondelete='cascade', required=True)
question_id = fields.Many2one(
'crm.question', string="Questions ID", ondelete='cascade', required=True)
checklist_item_id = fields.Many2one(
related='question_id.checklist_item_id', string="Checklist Item", store=True)
question_text = fields.Char(
related='question_id.question_text', store=True, string="Question")
answer = fields.Char(string="Answer", help="Answer specific to the lead/question combination")
crm_lead.py:
class CRMLead(models.Model):
_inherit='crm.lead'
_description = "Audits"
crm_lead_questions_test_ids = fields.One2many(
'crm.lead.question.answer',
'lead_id',
string="Question Answers"
)
# Using buttons
def generate_questions(self):
"""Generate question-answer records based on existing questions."""
for lead in self:
existing_question_ids = lead.crm_lead_questions_test_ids.mapped('question_id')
all_questions = self.env['crm.question'].search([])
for question in all_questions:
if question not in existing_question_ids:
self.env['crm.lead.question.answer'].create({
'lead_id': lead.id,
'question_id': question.id,
})
def action_generate_questions(self):
"""Generate missing question-answer records."""
self.generate_questions()
crm_lead_view.xml:
<?xml version="1.0"?>
<odoo>
<record id="crm_lead_view_form_stage_checklist" model="ir.ui.view">
<field name="name">crm.lead.view.form.stage.checklist</field>
<field name="model">crm.lead</field>
<field name="inherit_id" ref="crm.crm_case_form_view_oppor"/>
<field name="arch" type="xml">
<page string="Questions">
<group>
<button name="action_generate_questions" type="object" string="Generate Questions" class="btn-primary"/>
</group>
<group>
<field name="crm_lead_questions_test_ids" >
<tree editable="bottom">
<field name="question_id"/>
<field name="checklist_item_id"/>
<field name="question_text"/>
<field name="answer"/>
</tree>
<form>
<group>
<field name="question_id"/>
<field name="checklist_item_id"/>
<field name="question_text"/>
<field name="answer"/>
</group>
</form>
</field>
</group>
</page>