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

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.

I made a before-insert advanced BR with the following: 

johannaS_0-1668447630393.png

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

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.

Hitoshi Ozawa
Giga Sage
Giga Sage

FYI, attachments can be checked in Service Portal by checking "Mandatory Attachment" checkbox in Service Catalog form definition.

find_real_file.png

Hitoshi Ozawa
Giga Sage
Giga Sage

An easier way to make attachment mandatory is to make an attachment field and make the field mandatory.

find_real_file.png

Execution results:

(1) UI page.

find_real_file.png

(2) Service Portal

find_real_file.png