Save button override in sugar 7.9

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!

  • Hi Sarika Tandale,

    Instead of overriding a save button, why not using inbuilt validation. This doesn't require any extra effort to prevent save and even you can write it once and make it work for both create and record view. Please follow this link to get more idea - http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.8/User_Interface/Views/Adding_Field_Va… 

    Please let us know if this helps.

    Regards.

    Hats

  • Thank you this worked, however the record is saved only until the record is open on the same page. If I refresh the page, the record seems to be unsaved.

  • Hi Sarika Tandale,

    Glad to hear that you got it working. Are you calling model.save manually ? Would you mind sharing your code here so that we can have a look.

    Regards.

    Hats

  • Yes thank for responding back

    Here is my code:

    ({
    extendsFrom: 'RecordView',

    initialize: function(options) {
    this._super('initialize', [options]);
    this.model.addValidationTask('addressValidation', _.bind(this._validateCountry, this));

    },

    _validateCountry: function(fields, errors, callback) {
    // Getting the values of fields from sugar to pass them to the custom API endpoint
    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 USA, pass the correct parameter to the API
    if (country matches united states) {
    passCountry = 'US';
    } else {
    passCountry = 'usa';
    }

    $.ajax({
    type: 'GET',
    async: 'false',
    url: 'customURL',
    crossDomain: 'true',
    headers: {
    'key': 'value'
    },
    statusCode: {
    // If 200 response is received, set the sugar fields to the fields received from response.
    200: function(response) {
    cityNow = response.city;
    streetNow = response.street1;
    stateNow = response.state;
    zipNow = response.postalcode;
    countryNow = response.country;

    app.alert.show('address-ok', {
    level: 'success',
    messages: 'matches',
    autoClose: true
    });
    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 error response is received, do not change the sugar fields.
    400: function(response) {
    t.model.set('primary_address_city', currentCity);
    t.model.set('primary_address_street', currentStreet);
    t.model.set('primary_address_state', currentState);
    t.model.set('primary_address_postalcode', currentZip);
    t.model.set('primary_address_country', currentCountry);

    app.alert.show('address-ok', {
    level: 'error',
    messages: 'do not match',
    autoClose: false
    });
    }
    }
    })
    }

    })

    Everything is working fine except the save functionality- If there is an error, a message is shown and the record is not saved.

    If correct fields are given as input and save button is pressed, the fields are set properly as in the response given back by the services. The problem is if I try to refresh the page or leave the page, the content is not saved and I am getting a warning message - 

    "You have unsaved changes. Are you sure you want to leave page and discard changes?"

    Instead of getting this warning message, sugar should save the record after pressing save button.

  • Hi Sarika Tandale,

    You need to add callback function at the end so that sugar can continue the save process.

    So, if you look at the above screen shot, you need to define error in errors array and then at the end, you have to declare callback. According to your functionality, you can add errors[] in 400 status code and at the end do a callback.

    Let us know if this helps.

    Regards.

  • Yes it is working perfectly fine now!

    Thank you so much for helping me out

    Hope you have a good one.