Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
2096 Visualizzazioni

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
Abbandona
Risposta migliore

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
Abbandona
Post correlati Risposte Visualizzazioni Attività
3
giu 23
2486
1
mag 24
1517
1
set 23
2266
1
giu 23
3552
0
apr 23
1703