Search code examples
functionexportodooodoo-13

Show validation for export action click on specific condition Odoo 13


I want to show validation on the 'draft' stage that the user can not export draft stage data. I know about 'def export_data(self, fields_to_export)' function but it works after select fields. I want that validation just when clicking on export action. So, anyone can suggest me which function I used for my requirement. I am using Odoo 13.

Thanks in advance.


Solution

  • A way to do that is to override _onExportData of ListController.

    Check the following code (It uses state field):

    odoo.define("stack_overflow", function(require) {
    "use strict";
    
        var listController = require("web.ListController");
        var dialog = require("web.Dialog");
    
        listController.include({
             /**
             * Opens the Export Dialog
             *
             * @private
             */
            _onExportData: function () {
                var self = this;
                var do_export = true;
                // Avoid calling `read` when `state` field is not available
                if (self.initialState.fields.hasOwnProperty('state')) {
                    self._rpc({
                        model: self.modelName,
                        method: 'read',
                        args: [self.getSelectedIds(), ['state']],
                    }).then(function (result) {
                        // Check if we have at least one draft record
                        for(var index in result) {
                            var item = result[index];
                            if (item.state === 'draft') {
                                do_export = false;
                                break;
                            }
                        }
                        if (do_export) {
                            self._getExportDialogWidget().open();
                        } else {
                            dialog.alert(self, "You can't export draft stage data!", {});
                        }
                    });
                } else {
                    this._getExportDialogWidget().open();
                }
            },
        });
    
    });