- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2022 12:00 PM
I have the following scripts to check for an attachment and alert the user if it is missing and it should stop the form from saving or being submitted, but the last part (save and submit) still happen. See bellow.
function onSubmit() {
var sysid = g_form.getUniqueValue();
var deal_type = g_form.getValue('u_deal_type');
if (deal_type == 'rfp') {
var ga_cluter = new GlideAjax("PresalesDemandUtils");
ga_cluter.addParam("sysparm_name", "hasAttachment");
ga_cluter.addParam("sysparm_sys_id", sysid);
ga_cluter.getXML(CallBack);
}
function CallBack(response) {
var res = response.responseXML.documentElement.getAttribute("answer");
if (res == 'No Attachment Present') {
//g_form.submitted = false;
g_form.addErrorMessage('Attachment is needed for Deal Type RFP');
return false;
}
}
}
var PresalesDemandUtils = Class.create();
PresalesDemandUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
//Check if thor id is unique
validate_thor_id: function() {
var msg = "";
var gr_dem = new GlideRecord("x_caukp_presales_presales_demand");
gr_dem.addQuery("u_thor_id", this.getParameter("sysparm_thor_id"));
gr_dem.query();
if (gr_dem.next()) {
msg = "Not Unique";
} else {
msg = "Unique";
}
return msg;
},
getCluster: function() {
var groups = [];
var cluster = '';
var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('user', this.getParameter("sysparm_user_id"));
grMember.addQuery('group.name', 'ENDSWITH', 'cluster');
grMember.query();
while (grMember.next()) {
groups.push(grMember.group.name);
}
var grCluster = new GlideRecord("business_unit");
grCluster.addQuery('name', groups[0]);
grCluster.query();
if (grCluster.next()) {
cluster = grCluster.sys_id;
}
return cluster;
},
getClusterLead: function() {
var groups = [];
var clusterLead = '';
var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('user', this.getParameter("sysparm_user_id"));
grMember.addQuery('group.name', 'ENDSWITH', 'cluster');
grMember.query();
while (grMember.next()) {
groups.push(grMember.group.name);
}
var grCluster = new GlideRecord("business_unit");
grCluster.addQuery('name', groups[0]);
grCluster.query();
if (grCluster.next()) {
clusterLead = grCluster.bu_head;
}
return clusterLead;
},
hasAttachment: function() {
var msg = '';
var attach = new GlideRecord('sys_attachment');
attach.addQuery('x_caukp_presales_presales_demand', this.getParameter("sysparm_sys_id"));
attach.query();
if (attach.next()) {
msg = "Attachment Present";
} else {
msg = "No Attachment Present";
}
return msg;
},
type: 'PresalesDemandUtils'
});
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2022 04:31 PM
Hi JohannaS,
Can't use Ajax in onSubmit because the submit will process and be submitted before script include returns a value to client script.
Either change the logic to do the check in onChange() or do the check in before business rule.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2022 08:48 AM
I made an advanced business rule with the following script:
(function executeRule(current, previous /*null when async*/ ) {
var attach = new GlideRecord('sys_attachment');
attach.addQuery('table_sys_id', current.getUniqueValue());
attach.query();
if (!attach.hasNext()) {
gs.addErrorMessage("Attachment is needed for Deal Type RFP");
current.setAbortAction(true);
}
})(current, previous);
Let me know if you need any more information. Good luck.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2022 09:41 AM
I made a before-insert advanced BR with the following:
var attach = new GlideRecord('sys_attachment');
attach.addQuery('table_sys_id', current.getUniqueValue());
attach.query();
if (!attach.hasNext()) {
gs.addErrorMessage("Attachment is needed for Deal Type RFP");
current.setAbortAction(true);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2023 05:54 AM
Hi Hitoshi,
Can't we use Ajax within the OnLoad Client Script too ??? I have been trying it for a couple of days and it is not at all working.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2022 05:01 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2022 05:11 PM