Pular para o conteúdo
Menu
Esta pergunta foi sinalizada
1 Responder
2392 Visualizações

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
Cancelar
Melhor resposta

Hi,

Try to fetch the current record ID by:

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


Hope it helps

Avatar
Cancelar

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

Publicações relacionadas Respostas Visualizações Atividade
5
mar. 25
2226
1
fev. 25
924
1
fev. 24
2252
2
fev. 23
2724
3
jul. 20
12423