How can I prevent submit in onSubmit Catalog Client Scrip (Scoped App)?

johannaS
Tera Expert

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'
});

 

find_real_file.png

find_real_file.png

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

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.

View solution in original post

9 REPLIES 9

Aman Kumar S
Kilo Patron

Attaching one relevant article for your assistance:

https://community.servicenow.com/community?id=community_blog&sys_id=dc49feeadbdd7f0c54250b55ca96191d

 

Do let me know, if this works

Best Regards
Aman Kumar

Hitoshi Ozawa
Giga Sage
Giga Sage

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.

The business rule worked perfectly. Thank you.

Hello @johannaS what is the BR you created to solve this issue? I am also facing this kind of issue where the return false doesnt work to stop the form from submitting.

Thank you in advance!