On submit client script to stop record producer submittal

JJG
Kilo Guru

Hello, 

I have a client script that checks if there is a record when the record producer is submitted. It uses a script include to search a table for a record with the matching condition. If there is a matching record, the record producer should not submit.

Code isn't working, the record producer is not aborting the submit. What am I missing?

Client Script:

function onSubmit() {

var name = g_form.getValue('name_of_employee');

var gaCheck = new GlideAjax('checkPTO');
gaCheck.addParam('sysparm_name', 'check_pto');
gaCheck.addParam('sysparm_emp', name);
gaCheck.getXMLAnswer(_handleResponse); 

function _handleResponse(response) {
var answer = response;
answer = answer.toString();
if (answer == 'true') {
return false;
}}}

Script Include:

check_pto: function() {
var checkPTO = new GlideRecord('x_utsll_time_manag_pto_submittals');
checkPTO.addQuery('name_of_employee', this.getParameter('sysparm_emp'));
checkPTO.query();
if(checkPTO.next()) {
return true;
} else {
return false;
}
},

 

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi JJG,

This is correct. Depending on for example performance, sometimes your onSubmit Script and Script Include will perform fully, sometimes not. Reason: this is an onSubmit script, though you are also already submitting the record producer. So this onSubmit script does not stop the submission. The submission simply wins.

A workaround could be to have a checkbox variable, by default false. onSubmit check if the checkbox is true, if not, no submission and an error message.
Your actual script can probably run onChange? And so set the checkbox to true if it is correct. This way your original onSubmit will be possible with help of the checkbox variable.

Other way could be to dig in to getXMLWait. Though personally I wouldn't go that way.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

2 REPLIES 2

Mark Roethof
Tera Patron
Tera Patron

Hi JJG,

This is correct. Depending on for example performance, sometimes your onSubmit Script and Script Include will perform fully, sometimes not. Reason: this is an onSubmit script, though you are also already submitting the record producer. So this onSubmit script does not stop the submission. The submission simply wins.

A workaround could be to have a checkbox variable, by default false. onSubmit check if the checkbox is true, if not, no submission and an error message.
Your actual script can probably run onChange? And so set the checkbox to true if it is correct. This way your original onSubmit will be possible with help of the checkbox variable.

Other way could be to dig in to getXMLWait. Though personally I wouldn't go that way.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Allen Andreas
Administrator
Administrator

Yea, Mark gave some good points. My personal recommendation would be to create a sort of validation before you even get to submit it. Somewhat along the line with Mark's checkbox suggestion. If you've ever seen websites where you try to signup for an account and it checks to see if that username is taken or not? Sort of like that. So in order to submit, you need to pass a validation that has already taken place.

I always try to think of the end-user in mind, they spent time filling out the form, sometimes with excessive detail, then they try to submit and then are hit with the door telling them they can't enter due to failing some validation that could have taken place 15 fields prior...and not after they provided their favorite cereal and blood type and all that other information.

Using any script include in onSubmit just isn't a good idea unless...you purposely want it to run async and it has no effect on the current form being submitted, etc.

Using getXMLWait does not work on Service Portal, I believe.

Anyways, just wanted to toss out my 2 cents.

Take care!

Please mark reply as Helpful, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!