AnsweredAssumed Answered

Field validation doesn't have enough time to run

Question asked by Yury Voloshin on Apr 2, 2019
Latest reply on Apr 8, 2019 by Yury Voloshin

I'm trying to create a field validation in the Opportunity module that will throw an error when a user tries to change a commit stage from Upside or Commit back to Funnel. When the error is thrown, I expect the commit stage field to be highlighted and a pop-up to be displayed with an error message. The problem is that the validation error is thrown after the opportunity is already saved. I get a success-type pop-up saying "Saved", then I get a pop-up with the validation error message. The commit stage does not get highlighted. I'm guessing that the validation task takes too long to run, probably due to the OpportunityBean.fetch function, so that the record is saved before the validation task has time to throw an error. Is there a way to either delay saving, or to speed up getting data from the bean? Or, is the problem something different altogether?

My validation function in record.js looks like this:


initialize: function (options) {
    this._super('initialize', [options]);
    app.error.errorName2Keys['check_commit_stages'] = 'ERROR_CHECK_COMMIT_STAGES';
    this.model.addValidationTask('check_commit_stages', _.bind(this._doValidateCommitStages, this));

_doValidateCommitStages: function(fields, errors, callback) {
    console.log('Validating commit stages in record.js');

    let CommitStageSelected = this.model.get('commit_stage_c');
    let OppId = this.model.get('id');
    let OpportunityBean ='Opportunities');
    OpportunityBean.set('id', OppId);
    let CommitStageSaved = '';
    OpportunityBean.fetch({success: function(model, data){
            CommitStageSaved = OpportunityBean.get('commit_stage_c');
            console.log('selected commit stage: ' + CommitStageSelected);
            console.log('saved commit stage: ' + CommitStageSaved);

            let SavedStageUpsideOrCommit =
            ("Upside" == CommitStageSaved || "Commit" == CommitStageSaved);

            if ("Funnel" == CommitStageSelected && SavedStageUpsideOrCommit) {
                errors['commit_stage_c'] = errors['commit_stage_c'] || {};
                errors['commit_stage_c'].check_commit_stages = true;

      'message-id', {
                    level: 'error',
                    messages: 'Commit Stage cannot be changed from Upside or from Commit to Funnel',
                    autoClose: false

            callback(null, fields, errors);