我正在尝试在商机模块中创建字段验证,当用户尝试将提交阶段从“上端”或“提交”更改为“漏斗”时,将引发错误。当用户尝试保存已编辑的机会时,record.js
中的验证功能将运行。它将选定的提交阶段与数据库中保存的提交阶段进行比较。如果选定的提交阶段为“渠道”,并且保存的提交阶段为“上流”或“提交”,则会引发验证错误。抛出验证错误时,应突出显示提交阶段字段,并显示带有错误消息的弹出窗口。
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);
}});
},
问题在于机会已经保存之后引发了验证错误。我得到一个“成功”弹出窗口,说“已保存”,然后得到一个“错误”弹出窗口,其中包含验证错误消息。提交阶段字段不会突出显示。
我猜想验证任务的运行时间太长,这可能是由于OpportunityBean.fetch
函数所致,因此在验证任务有时间引发错误之前保存了记录。有没有办法延迟保存或加快从Bean获取数据的方法?或者,这个问题是否完全不同?
答案 0 :(得分:1)
我认为您也可以针对要完成的任务尝试这种方法
({
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即可从数据库中获取保存的数据。
答案 1 :(得分:0)
感谢所有受访者!唯一对我有用的是:
let changedFields = this.model.changedAttributes(this.model.getSynced());
let CommitStageSaved = changedFields.commit_stage_c;
当我尝试@Sheikh Rahat Ali的答案中的方法时,我得到CommitStageSaved =未定义。当我尝试@Jay的方法并将提取功能限制为commit_stage_c
字段时,在验证运行之前保存记录时遇到了同样的问题。我在Sugar开发者论坛上的response to my question中了解了changedAttributes
函数。