Se rendre au contenu
Menu
Cette question a été signalée
1 Répondre
1221 Vues

Hello! I'm somewhat new to odoo and I have this problem:

I need to show a confirmation dialog box when there is no email in a specific model when saving a form, I achieved this by modifying the "saveButtonClicked" function and creating "saveConfirmationDialogProps" as shown below:


get saveConfirmationDialogProps() {

        return {            

​body: this.env._t("¿Estas seguro que quieres guardar este registro sin ​email?"), 

           ​ confirm: async () => {                                

​const record = this.model.root;

​let saved = false;               

​ if (this.props.saveRecord) {                    

​saved = await this.props.saveRecord(record, params);       

​} else {                   

​ saved = await record.save();               

​}                

​if (saved && this.props.onSave) {     

​this.props.onSave(record, params);                

​}            

​},            

​cancel: () => {},        

​};   

 }


async saveButtonClicked(params = {}) {       

​ const record = this.model.root;        

​ let saved = false;        

​ console.log(record);        

​ if (record.resModel === "res.partner" && record.data.email === "") {            ​this.dialogService.add(ConfirmationDialog, ​this.saveConfirmationDialogProps);        

​} else {             

​if (this.props.saveRecord) {                

​saved = await this.props.saveRecord(record, params);            

​} else {               

​ saved = await record.save();            

​}           

​ if (saved && this.props.onSave) {                

​this.props.onSave(record, params);            

​}        

​}       

​ return true;    

}  


This in the form_controller.js file in this path: "C:\Program Files\Odoo 16\server\odoo\addons\web\static\src\views\form\form_controller.js" where I have odoo installed


But I should not modify the odoo source file, I should overwrite it from a script that is in my custom module that is located in another path.


This is the code I implemented:


odoo.define('felsv_parameter.custom_script', function (require) {   

​"use strict";

    ​const FormController = require('web.FormController');   

​const ConfirmationDialog=require('@web/core/confirmation_dialog/confirmation_dialog');    ​alert("Script works")

    ​var CustomFormController = FormController.include({

  ​async saveButtonClicked(params = {}) {            

​alert("save works")            

​​const record = this.model.root;            

​​let saved = false;            

​​if (record.resModel === "res.partner" && record.data.email === "") {         

​​const confirmationDialogProps = {                   

​​body: this.env._t("¿Estás seguro de que deseas guardar este registro sin ​correo ​​electrónico?"),                    

​​ ​confirm: async () => {

​​if (this.props.saveRecord) {                            

​​saved = await this.props.saveRecord(record, params);                

​} else {                            

​saved = await record.save();                        

​​}                        

​​​if (saved && this.props.onSave) {                            

​​this.props.onSave(record, params);                        

​​​}                    

​​​},                    

​​cancel: () => {},                

​​​};                

​​​this.dialogService.add(ConfirmationDialog, confirmationDialogProps);            

​​} else { 

​​if (this.props.saveRecord) {                    

​​saved = await this.props.saveRecord(record, params);                

​​} else {                    

​​saved = await record.save();                

​​}                

​​if (saved && this.props.onSave) {                    

​​this.props.onSave(record, params);                

​​}            

​​}            

​​return true;        

​},    

​});   

​console.log("finish");    

​return CustomFormController;

});


The script is executed because it shows me the alert and the console.log at the end but it doesn't overwrite the function because it doesn't show me the second alert("save works") and it doesn't have the behavior that I initially achieved by modifying the odoo file, I have already tried replacing "include" with "extend" but it still doesn't work. 

Does anyone know what I can do?


 




Avatar
Ignorer
Meilleure réponse

Hi,

Try this : export default class CustomFormController extends FormController {

        async saveButtonClicked(params = {}) {            


alert("save works")            


const record = this.model.root;            


let saved = false;            


if (record.resModel === "res.partner" && record.data.email === "") {        


const confirmationDialogProps = {                  


body: this.env._t("¿Estás seguro de que deseas guardar este registro sin correo electrónico?"),                    


confirm: async () => {


if (this.props.saveRecord) {                            


saved = await this.props.saveRecord(record, params);                


} else {                            


saved = await record.save();                        


}                        


if (saved && this.props.onSave) {                            


this.props.onSave(record, params);                        


}                    


},                    


cancel: () => {},                


};                


this.dialogService.add(ConfirmationDialog, confirmationDialogProps);            


} else {


if (this.props.saveRecord) {                    


saved = await this.props.saveRecord(record, params);                


} else {                    


saved = await record.save();                


}                


if (saved && this.props.onSave) {                    


this.props.onSave(record, params);                


}            


}            


return true;        


},    


});    


console.log("finish");    


}


Hope it helps

Avatar
Ignorer
Publications associées Réponses Vues Activité
3
juin 23
1855
1
mai 24
959
1
sept. 23
1657
1
juin 23
2949
0
avr. 23
1402