AnsweredAssumed Answered

Save button override in sugar 7.9

Question asked by Sarika Tandale on Jan 8, 2018
Latest reply on Jan 11, 2018 by Sarika Tandale

Hello,

I am trying to override a save button. The fields entered are checked by performing validations on them. If the response received from the API is "200" ( correct fields are entered by the user), only then should the record be saved back in sugar. If the response received is other than 200 (incorrect fields are entered), an error message should be presented to the user and the record should not be saved.

 

What I have already achieved -

I am able to call a function when the save button is clicked. This function is validating the fields correctly. If 200 response is received from the API, the record is being saved correctly.

 

What I want to achieve -

I am not able to override the save button and irrespective of the "400" response received from the API, the record is being saved in sugar. So if the fields are entered correctly, the record is being saved ( Yes that is the ultimate target). But if the fields entered are incorrect, the record is still being saved ( This is the issue I am unable to resolve).
Following is my code:

../custom/modules/Contacts/clients/base/views/record/record.js :

 

 

({
extendsFrom: 'EditView',

initialize: function(options) {
this._super('initialize', [options]);
this.context.on('button:save_button:click', this.validate_country, this);

},

validate_country: function(response) {

var t = this;
var currentStreet = this.model.get('primary_address_street');
var currentCity = this.model.get('primary_address_city');
var currentState = this.model.get('primary_address_state');
var currentZip = this.model.get('primary_address_postalcode');
var currentCountry = this.model.get('primary_address_country');

var encodedStreet = encodeURI(currentStreet);
var encodedCity = encodeURI(currentCity);
var encodedState = encodeURI(currentState);
var encodedZip = encodeURI(currentZip);

 

if(the country is united states){
// pass a correct country parameter to the API

passCountry = 'US';

}
else{
// pass a wrong country parameter to the API

passCountry = 'notUS';
}


$.ajax({
type: 'GET',
async: 'false',
url: 'someURL',
crossDomain: 'true',
headers: {'key': 'value'},


success: function(response) {
cityNow = response.city;
streetNow = response.street1;
stateNow = response.state;
zipNow = response.postalcode;
countryNow = response.country;

t.model.set('primary_address_city',cityNow);
t.model.set('primary_address_street',streetNow);
t.model.set('primary_address_state',stateNow);
t.model.set('primary_address_postalcode',zipNow);
t.model.set('primary_address_country',countryNow);

if (AJAX sends 200 response)
{
app.alert.show('address-ok', {
level: 'success',
messages: 'matches',
autoClose: false
});
}

},

// This error function is getting executed but the record is still saved in sugar
error: function(response) {
if(AJAX sends response other than 200){
app.alert.show('address-ok', {
level: 'error',
messages: 'do not match',
autoClose: true
});
}
}

 

})
}

})

 

Thank you in advance!

Outcomes