需求:检查该记录是否有附件。如果是,则允许用户撤销该记录。如果否,则向用户显示错误消息。
在作用域应用程序中运行下面的工作区客户端脚本,单击按钮时出现错误
工作区客户端脚本
function onClick(g_form) {
// Check if there are attachments
checkAttachments(g_form.getUniqueValue(), function(attachmentCount) {
attachmentCount = parseInt(attachmentCount);
if (attachmentCount > 0) {
// If there are attachments, confirm retirement
var msg = getMessage('When a policy is retired, associated policy acknowledgement campaign is canceled, certain control objectives are also deactivated, and related controls are retired. Are you sure you want to continue?');
g_modal.confirm(getMessage('Confirmation'), msg).then(
function onSuccess() {
g_form.submit('policy_retire');
}
);
} else {
// If there are no attachments, show a warning message
g_form.addErrorMessage("Please add an attachment before retiring the policy.");
}
});
return false;
}
function checkAttachments(sysId, callback) {
var ga = new GlideAjax('CheckAttachmentsAjax');
ga.addParam('sysparm_name', 'checkAttachments');
ga.addParam('sysparm_sysid', sysId);
ga.getXML(function(response) {
var attachmentCount = parseInt(response.responseXML.documentElement.getAttribute("answer"));
callback(attachmentCount);
});
}
脚本包括:
var CheckAttachmentsAjax = Class.create();
CheckAttachmentsAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
checkAttachments: function() {
var sysId = this.getParameter('sysparm_sysid');
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'sn_compliance_policy');
attachmentGR.addQuery('table_sys_id', sysId);
attachmentGR.query();
var attachmentCount = attachmentGR.getRowCount();
return attachmentCount;
},
type: 'CheckAttachmentsAjax'
});
将脚本包含移动到全局范围修复了它