Skip to Content
Menú
This question has been flagged
2 Respostes
770 Vistes

I have transferred the _track_subtype method from the CRM module to my module and inserted the is_lost field, which changes the status to true when the lead is moved to the stage with the label lost. It is just that the state does not always change. If I now move it back to another stage, it remains lost with the heading at the top right. Only when I refresh the page is the state updated and the heading disappears. 


How can I reload the Kanban view in Odoo after moving a lead to a different stage?


class Lead(models.Model):

    _inherit = 'crm.lead'

   

    is_lost = fields.Boolean(string='Is Lost', default=False)

   

    def _track_subtype(self, init_values):

        self.ensure_one()


        if self.stage_id.name.find("Lost") != -1:

            # If a lead is moved to Lost, then set is_lost to True

            self.is_lost = True

           

        else:

            # For all other lead set is_lost to False           

            self.is_lost = False     

       

        if 'stage_id' in init_values and self.probability == 100 and self.stage_id:

            return self.env.ref('crm.mt_lead_won')

        elif 'lost_reason_id' in init_values and self.lost_reason_id:

            return self.env.ref('crm.mt_lead_lost')

        elif 'stage_id' in init_values:

            return self.env.ref('crm.mt_lead_stage')

        elif 'active' in init_values and self.active:

            return self.env.ref('crm.mt_lead_restored')

        elif 'active' in init_values and not self.active:

            return self.env.ref('crm.mt_lead_lost')

        return super(Lead, self)._track_subtype(init_values)


<record id="mymodule_crm_case_kanban_view_leads" model="ir.ui.view">

    <field name="name">mymodule.crm.lead.kanban.lead</field>

    <field name="model">crm.lead</field>

    <field name="inherit_id" ref="crm.crm_case_kanban_view_leads"/>

    <field name="arch" type="xml">

        <!--  Aperture with red background and white lettering 'Lost' in the window -->

        <xpath expr="//widget[@name='web_ribbon']" position="replace">

            <field name="is_lost" invisible="0"/>

            <widget name="web_ribbon" title="lost" bg_color="text-bg-danger" invisible="is_lost == False"/>

        </xpath>

    </field>

</record>

Avatar
Descartar
Autor

Hi, there is still the error, now it is not updated when i move it back to lost. i think i have to use javascript here. i use odoo 18. how can i integrate javascript. i don't know anything about this. Could you describe this in detail.

Autor Best Answer

I have tried to automatically reload the Kanban view in Odoo 18 when a data record (e.g. a lead) is moved via drag & drop:


Javascript:

import { patch } from '@web/core/utils/patch';


patch(KanbanController.prototype, {

    async dropRecord(record, targetColumn) {

        await this._super(...arguments);


        if (this.model.root.resModel === 'crm.lead') {

            await this.model.load(); 

            this.render(true);    

        }

    }

});


Manifest:'assets': {

    'web.assets_backend': [

        'my_module/static/src/js/kanban_stage_change_refresh.js',

    ],

},

This is a code that was created with Copilot. Is it sufficient to insert the Javscript and change the manifest? Because it doesn't work when I try.

Avatar
Descartar
Best Answer

Hey,

You’re on the right track by inheriting _track_subtype and managing the is_lost flag based on the stage. The behavior you’re seeing—where the ribbon doesn’t update until a page refresh—comes down to how Odoo handles view updates in the Kanban.

Odoo doesn’t automatically re-render the whole Kanban card when a field like is_lost is changed via Python unless that field is part of a computed or onchange method triggered by the UI. Since is_lost is being set in backend logic, the frontend doesn’t get the update immediately.

How to fix it?

To force the UI to reflect the is_lost status change immediately when you move the lead:

Use a computed field with store=True and make it depend on the stage_id. That way, the ORM knows to recompute and store the value when the stage changes.

@api.depends('stage_id')

def _compute_is_lost(self):

    for lead in self:

        lead.is_lost = 'lost' in (lead.stage_id.name or '').lower()


is_lost = fields.Boolean(string='Is Lost', compute='_compute_is_lost', store=True)

Make sure the Kanban view includes the is_lost field directly (which you’ve already done with the ribbon), so it knows to watch for updates on that field.

This way, as soon as you move the lead to a different stage, Odoo will recompute the is_lost value and update the UI accordingly—no need to manually refresh the page.

Let me know if you want to go one step further and make it dynamic with JS or override write() instead. But for 99% of cases, the computed field with store=True handles it perfectly.

Avatar
Descartar
Related Posts Respostes Vistes Activitat
0
de jul. 25
105
1
de jul. 25
228
1
de jul. 25
385
0
de jul. 25
359
1
de juny 25
417