Zum Inhalt springen
Menü
Sie müssen registriert sein, um mit der Community zu interagieren.
Diese Frage wurde gekennzeichnet
1 Antworten
2900 Ansichten

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
Verwerfen
Beste Antwort

Hi,

Try to fetch the current record ID by:

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


Hope it helps

Avatar
Verwerfen

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

Verknüpfte Beiträge Antworten Ansichten Aktivität
5
März 25
2845
1
Feb. 25
1367
1
Feb. 24
2762
2
Feb. 23
3018
3
Juli 20
12725