Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
1 ตอบกลับ
2097 มุมมอง

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.

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hi,

Try to fetch the current record ID by:

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


Hope it helps

อวตาร
ละทิ้ง

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

Related Posts ตอบกลับ มุมมอง กิจกรรม
5
มี.ค. 25
1895
1
ก.พ. 25
649
1
ก.พ. 24
1952
Controller not working แก้ไขแล้ว
2
ก.พ. 23
2434
How to get model object from Controller แก้ไขแล้ว
3
ก.ค. 20
12181