Search code examples
javascriptodoorpcodoo-11

Python function rpc method called late than do_action() in js file Odoo 11


I'm added button on the timesheet list view and opened wizard on button click. For this, I'm called python in the js file and when I pass function value in do_action() method(for res_id) of js then it will open wizard in the second attempt, in the first attempt it open the new wizard. It means(this.do_action called before rpc.query) function calling late. The expected result should be rpc.query call before this.do_action. variable defined as 'test'.

My Python and Js code are here:

class TimesheetHelpButton(models.Model):
    _name = 'timesheet.help'
    _description = 'Timesheet Help Button'

    @api.model
    def get_timesheet_help_document(self):
        uid = request.session.uid
        #timesheet_document_view_id = self.env['document.document'].sudo().search([])
        data = {
            'uid': uid,
            'timesheet_document_view_id': 4,
        }
        return data

Js Code:

odoo.define('custom_project.web_export_view', function (require) {

"use strict";       
var core = require('web.core');
var ListView = require('web.ListView'); 
var ListController = require("web.ListController");
var rpc = require('web.rpc');
var test = 0;

var includeDict = {

    renderButtons: function () {
        this._super.apply(this, arguments);
        if (this.modelName === "account.analytic.line") {
            var your_btn = this.$buttons.find('button.o_button_help')
            your_btn.on('click', this.proxy('o_button_help'))
        }
    },
    o_button_help: function(){
        var self = this;
        event.stopPropagation();
        event.preventDefault();
        rpc.query({
            model: 'timesheet.help',
            method: 'get_timesheet_help_document',
            args: [],
        }).then(function (res) {
                    test = res['timesheet_document_view_id'];
                    }).done(function(){
            });
        setTimeout(myfonction, 5000);
        function myfonction() {}
        this.do_action({
            name: ("Help"),
            type: 'ir.actions.act_window',
            res_model: 'document.document',
            view_mode: 'form,tree,kanban',
            view_type: 'form',
            views: [[false, 'form'],[false, 'list'],[false, 'kanban']],
            target: 'new',
            res_id: test,
        },{on_reverse_breadcrumb: function(){ return self.reload();}})
    },

};
ListController.include(includeDict);
});

Also find screenshots below: enter image description here enter image description here

Thanks in advance


Solution

  • can you try to write your function like that

    o_button_help: function(){
            var self = this;
            event.stopPropagation();
            event.preventDefault();
            rpc.query({
                model: 'timesheet.help',
                method: 'get_timesheet_help_document',
                args: [],
            }).then(function (res) {
                test = res['timesheet_document_view_id'];
                self.do_action(
                    {
                        name: ("Help"),
                        type: 'ir.actions.act_window',
                        res_model: 'document.document',
                        view_mode: 'form,tree,kanban',
                        view_type: 'form',
                        views: [[false, 'form'],[false, 'list'],[false, 'kanban']],
                        target: 'new',
                        res_id: test,
                    },
                    {
                        on_reverse_breadcrumb: function(){ return self.reload();}
                    }
                )
            });
    
        },