Skip to Content
Menú
This question has been flagged
1 Respondre
2888 Vistes

I'm using Odoo 17 with the OWL framework and have added a custom button in the sale.order form view using FormController . The button should fetch data from res.partner and fill fields in the current sale.order form view. However, it fetches the ID of the previous record instead of the current one when clicked.

/** @odoo-module */

import { FormController } from "@web/views/form/form_controller";

import { formView } from '@web/views/form/form_view';

import { registry } from '@web/core/registry';

import { useService } from "@web/core/utils/hooks";

import { browser } from "@web/core/browser/browser";


const { useState } = owl;


const _t = (message) => message;


export class SaleFormController extends FormController {

    set up() {

        super.setup();

        this.actionService = useService("action");

        this.notificationService = useService("notification");


        this.state = useState({

            rpc_data: [],

        });

    }


    async getRpcService() {

        const rpc = this.env.services.rpc;

        const saleOrderId = this.props.resId; // Getting the current record ID


        if (!saleOrderId) {

            this.notificationService.add(_t("Sale Order ID is undefined."), {

                title: "Error",

                type: "danger"

            });

            return;

        }


        try {

            const data = await rpc("/owl/rpc_service", {

                sale_order_id: saleOrderId,

                limit: 15,

            });


            if (data === "success") {

                this.state.rpc_data = data.items;

                this.notificationService.add(_t("Data successfully retrieved. Reloading..."), {

                    title: "Success",

                    type: "success"

                });


                setTimeout(() => {

                    browser.location.reload();

                }, 2000);

            } else {

                this.notificationService.add(_t("Error retrieving data."), {

                    title: "Error",

                    type: "danger"

                });

            }

        } catch (error) {

            this.notificationService.add(_t("RPC call failed: " + error.message), {

                title: "Error",

                type: "danger"

            });

        }

    }

}


SaleFormController.template = 'button_sale.FormView.Buttons';

export const modelInfoView = {

    ...formView,

    Controller: SaleFormController,

};

registry.category("views").add("button_in_form", modelInfoView);


I'm seeking guidance on why the button fetches the previous record's ID instead of the current one and how to ensure it fetches the correct active ID. Any insights would be greatly appreciated.

Avatar
Descartar
Best Answer

Hi,

Try to fetch the current record ID by:

const saleOrderId = this.model.root.resId; // Getting the current record ID


Hope it helps

Avatar
Descartar

Root and ResId are not defined. Could you provide full demo code? Thanks

Related Posts Respostes Vistes Activitat
5
de març 25
2842
1
de febr. 25
1364
1
de febr. 24
2758
2
de febr. 23
3013
3
de jul. 20
12722