Events on field change

I am working on Odoo 16 and I am trying to add a confirmation alert when someone tries to change the stage of any record using the statusbar widget.

enter image description here

Basically, when you click on any stage, it switches to that one, but what I want is that when you try to switch stages, there is a modal/alert where you have to confirm or cancel the changes, and I want it to work on ALL status bars of ANY model. That's it.

I tried a lot of things, but nothing worked. I created a new custom module and tried the following as a test:

custom_addons/custom_stage_confirmation/static/src/js/stage_confirmation.js:

/** @odoo-module **/

import { patch } from "@web/core/utils/patch";
import { registry } from "@web/core/registry";
import { FormController } from "@web/views/form/form_controller";

// Obtener el registro de vistas (viewRegistry)
const viewRegistry = registry.category('views');

// Parchear el controlador del formulario
patch(FormController.prototype, 'custom_stage_confirmation', {
    async _onFieldChanged(event) {
        // Llamar al super para mantener la funcionalidad original
        await this._super(...arguments);

        // Verificar si el campo 'stage_id' ha sido cambiado
        if (event.data.changes && event.data.changes.stage_id) {
            const newStageId = event.data.changes.stage_id[1]; // Obtiene el nombre del nuevo stage
            console.log('El stage_id ha cambiado a:', newStageId); // Log del nuevo stage_id
            this.displayNotification({
                type: 'info',
                title: 'Cambio de etapa',
                message: 'El stage_id ha cambiado a: ' + newStageId,
            });
        } else {
            console.log('No se ha cambiado el stage_id.');
        }
    },
});

// Registrar la vista para asegurarse de que el controlador parcheado se utiliza en la vista de formulario
viewRegistry.add('stage_confirmation', FormController);

In the manifest.py:

# -*- coding: utf-8 -*-
{
    'name': "custom_stage_confirmation",
    'author': "Marín",
    'category': 'Uncategorized',
    'version': '0.1',
    'depends': ['web', 'base'],
    'data': [
        # 'security/ir.model.access.csv',
        'views/views.xml',
        'views/templates.xml',
    ],
    'assets': {
        'web.assets_backend': [
            'custom_stage_confirmation/static/src/js/stage_confirmation.js',
        ],
    },
}

Note: All the scripts are loaded, and there are no errors, this simply doesn't do anything.

I don't know what to do, there is no documentation on FormController, which is the closest thing I found, but it doesn't work.

Upvotes: 0

Views: 85

Answers (1)

Qasim Qasim
Qasim Qasim

Reputation: 1

You need to use a patch KanbanController and use hook onRecordChanged and add a promise.

Upvotes: -1

Related Questions