如何确保 Sugar 字段验证有足够的时间 运行

How to make sure that Sugar field validation has enough time to run

我正在尝试在机会模块中创建一个字段验证,当用户尝试将提交阶段从 "Upside" 或 "Commit" 更改回 [=25= 时会抛出错误].当用户尝试保存已编辑的机会时,record.js 运行 中的验证函数。它将选定的提交阶段与保存在数据库中的提交阶段进行比较。如果选定的提交阶段是 "Funnel" 并且保存的提交阶段是 "Upside" 或 "Commit",则会抛出验证错误。抛出验证错误时,应突出显示提交阶段字段,并显示带有错误消息的弹出窗口。

record.js 中的验证码如下所示:

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 OpportunityId = this.model.get('id');
    let OpportunityBean = app.data.createBean('Opportunities');
    OpportunityBean.set('id', OpportunityId);
    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;

                app.alert.show('message-id', {
                    level: 'error',
                    messages: 'Commit Stage cannot be changed from Upside or from Commit to Funnel',
                    autoClose: false
                });
            }

            callback(null, fields, errors);
    }});
},

问题是在机会已保存后抛出验证错误。我收到一个 "success" 弹出窗口,上面写着 "Saved",然后我收到一个带有验证错误消息的 "error" 弹出窗口。提交阶段字段不会突出显示。

我猜测验证任务花费的时间太长 运行,可能是由于 OpportunityBean.fetch 函数,因此记录在验证任务有时间抛出一个错误。有没有办法延迟保存,或者加快从 bean 获取数据的速度?或者,问题完全不同吗?

我认为您也可以尝试使用这种方法来完成您要完成的任务

({
extendsFrom: 'OpportunitiesRecordView',

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 CommitStageSaved = this.model.getSynced('commit_stage_c');
    let CommitStageSelected = this.model.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;

        app.alert.show('message-id', {
            level: 'error',
            messages: 'Commit Stage cannot be changed from Upside or from Commit to Funnel',
            autoClose: false
        });
    }

    callback(null, fields, errors);
}, })

使用这种方法,您甚至不必获取 Opportunities bean 即可从数据库中获取保存的数据。

感谢所有回复者!唯一对我有用的是:

let changedFields = this.model.changedAttributes(this.model.getSynced());
let CommitStageSaved = changedFields.commit_stage_c;

当我尝试@Sheikh Rahat Ali 的回答中的方法时,我得到 CommitStageSaved = undefined。当我尝试@Jay 的方法并将获取功能限制为仅 commit_stage_c 字段时,我在验证 运行 之前保存记录时遇到了同样的问题。我从 Sugar 开发者论坛的 response to my question 中发现了 changedAttributes 函数。