Skip to Content
Menu
This question has been flagged
1 Reply
2182 Views

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
Discard
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
Discard

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

Related Posts Replies Views Activity
5
Mar 25
1969
1
Feb 25
747
1
Feb 24
2067
2
Feb 23
2523
3
Jul 20
12265