AnsweredAssumed Answered

How to stop save action if some condition become false

Question asked by Maulik Shah on Dec 29, 2014
Latest reply on Feb 8, 2018 by Rodrigo Morchio
Hi All,

I am trying to put duplicate validation check on save action for email field, so if an email is already used for some other contact it should show error message and should prevent save action.
So, i added following code to custom/modules/Contacts/clients/base/views/record/record.js

({
    extendsFrom: 'ContactsRecordView',
    initialize: function (options) {          this._super('initialize', [options]);  this.context.on('button:save_button:click', this.checkExistEmail, this);  },  checkExistEmail: function () {          debugger;          var emailVal = new Array('no_email');          var record = this.model.get('id');          var is_new_contact = 'yes';          if (record != '') {              is_new_contact = 'no';          }          var emails = this.model.get('email');          $(emails).each(function (index) {              debugger;              emailVal[index] = emails[index].email_address;          });          var arg = 'Contacts/' + record + '/' + emailVal + '/' + is_new_contact + '/check_exist_email';          var response = this.checkEmailByController(arg);          if (response != '1') {              return false;          }      },      checkEmailByController: function (arg) {          app.api.call('GET', app.api.buildURL(arg), null,                  {                      success: function (email) {                          if (email != '') {                              app.alert.show('Email Exist Message', {                                  level: 'error',                                  messages: 'Email "' + email + '" is already exist'}                              );                          }                      }                  }          );          console.log('Step 3');      }  });

can anyone please tell me how can i stop default save action if check returns true (1) so it does not save contact?

An existing logic which checks for valid email address does stop the save action on entering invalid email address and i want same logic for duplicate check.

Using sugarcrm pro 7.2.1.

Thanks

Outcomes