- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-19-2024 11:08 AM
Hello Team
I'm writing a script include to validate the catalog form.
If the input values match with record on the table then alert the user that "our database is showing that you already registered". And clear the value.
I also wrote the catalog client script to response from script include. I use onSubmit for client script. I don't know if that method is right or not. It sounds right. I did some testing. It didn't do anything when i hit submit. I'm not sure why it did do the alert pop up. Thanks for your help
Script include
//define the function
getStudentRegistrationDetails: function() {
var studentOrInstructor = this.getParameter('sysparm_studentOrInstructor');
var firstName = this.getParameter('sysparm_fname');
var lastName = this.getParameter('sysparm_lname');
var email = this.getParameter('sysparm_email');
var phoneNumber = this.getParameter('sysparm_phoneNumber');
var currentAddress = this.getParameter('sysparm_currentAddress');
var city = this.getParameter('sysparm_city');
var state = this.getParameter('sysparm_state');
var zipcode = this.getParameter('sysparm_zipcode');
//query the registration table
var ga = new GlideRecord('x_74571_kb_univ_ku_registration');
ga.addQuery('student_or_instructor', studentOrInstructor);
ga.addQuery('first_name', firstName);
ga.addQuery('last_name', lastName);
ga.addQuery('email', email);
ga.addQuery('phone_number', phoneNumber);
ga.addQuery('current_address', currentAddress);
ga.addQuery('city', city);
ga.addQuery('state', state);
ga.addQuery('zipcode', zipcode);
ga.query();
while(ga.next())
{
if (ga.getValue('student_or_instructor') === studentOrInstructor &&
ga.getValue('first_name') === firstName &&
ga.getValue('last_name') === lastName &&
ga.getValue('email') === email &&
ga.getValue('phone_number') === phoneNumber &&
ga.getValue('current_address') === currentAddress &&
ga.getValue('city') === city &&
ga.getValue('state') === state &&
ga.getValue('zipcode') === zipcode) {
output = true;
break;
}
}
return output;
},
Catalog Client Script
function onSubmit() {
//Type appropriate comment here, and begin script below
//Get the value in each of variables that you want from the form field
var studentOrinstructor = g_form.getValue('student_or_instructor');
var fname = g_form.getValue('first_name');
var lname = g_form.getValue('last_name');
var email = g_form.getValue('email');
var phoneNumber = g_form.getValue('phone_number');
var currentAddress = g_form.getValue('current_address');
var city = g_form.getValue('city');
var state = g_form.getValue('state');
var zipcode = g_form.getValue('zipcode');
//Create an Ajax Object
var gr = new GlideAjax('x_74571_kb_univ.SNO_AC_getAllFunctions');
gr.addParam('sysparm_name',"getStudentRegistrationDetails");
gr.addParam("sysparm_student_or_instructor", studentOrinstructor);
gr.addParam("sysparm_fname", fname);
gr.addParam("sysparm_lname", lname);
gr.addParam("sysparm_email", email);
gr.addParam("sysparm_phoneNumber", phoneNumber);
gr.addParam("sysparm_currentAddress", currentAddress);
gr.addParam("sysparm_city", city);
gr.addParam("sysparm_state", state);
gr.addParam("sysparm_zipcode", zipcode);
gr.getXML(updateStudentRegistration);
//Response the function
function updateStudentRegistration(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'true') {
alert('Our database is showned you already registered as student. ');
g_form.clearValue('first_name', true);
g_form.clearValue('last_name', true);
g_form.clearValue('email', true);
g_form.clearValue('phone_number', true);
g_form.clearValue('current_address', true);
g_form.clearValue('city', true);
g_form.clearValue('state', true);
g_form.clearValue('zipcode', true);
}
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2024 11:57 PM
Ok. i updated my script into your script. It still didn't prevent the submit button. It got the alert that is duplication but it is still going through the request. I'm not sure if I have anything wrong with this workaround. Thanks for all your help !
function onSubmit() {
if (g_scratchpad.isformValid) {
return true;
}
//Get the value in each of the variables that you want from the form field
var studentOrinstructor = g_form.getValue('student_or_instructor');
var fname = g_form.getValue('first_name');
var lname = g_form.getValue('last_name');
var email = g_form.getValue('email');
var phoneNumber = g_form.getValue('phone_number');
var currentAddress = g_form.getValue('current_address');
var city = g_form.getValue('city');
var state = g_form.getValue('state');
var zipcode = g_form.getValue('zipcode');
var gr = new GlideAjax('x_74571_kb_univ.SNO_AC_getAllFunctions');
gr.addParam('sysparm_name', 'getStudentRegistrationDetails');
gr.addParam('sysparm_student_or_instructor', studentOrinstructor);
gr.addParam('sysparm_fname', fname);
gr.addParam('sysparm_lname', lname);
gr.addParam('sysparm_email', email);
gr.addParam('sysparm_phoneNumber', phoneNumber);
gr.addParam('sysparm_currentAddress', currentAddress);
gr.addParam('sysparm_city', city);
gr.addParam('sysparm_state', state);
gr.addParam('sysparm_zipcode', zipcode);
gr.getXML(updateStudentRegistration);
//Response the function
function updateStudentRegistration(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'true') {
alert('Our database is shown you already registered as student. ');
g_form.clearValue('first_name', true);
g_form.clearValue('last_name', true);
g_form.clearValue('email', true);
g_form.clearValue('phone_number', true);
g_form.clearValue('current_address', true);
g_form.clearValue('city', true);
g_form.clearValue('state', true);
g_form.clearValue('zipcode', true);
g_scratchpad.isformValid = true;
g_form.submit(g_form.getActionName());
} else {
alert("No Record:" + answer);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-19-2024 11:37 AM
Hello @SnowDevOps ,
There is no need to check all your attributes with your actual variables output as you are already applying the filter condition to match. So try the below and see.
// Script Include - replace this inside your fun.
//define the function
getStudentRegistrationDetails: function() {
var studentOrInstructor = this.getParameter('sysparm_student_or_instructor'); // Here is an error before
var firstName = this.getParameter('sysparm_fname');
var lastName = this.getParameter('sysparm_lname');
var email = this.getParameter('sysparm_email');
var phoneNumber = this.getParameter('sysparm_phoneNumber');
var currentAddress = this.getParameter('sysparm_currentAddress');
var city = this.getParameter('sysparm_city');
var state = this.getParameter('sysparm_state');
var zipcode = this.getParameter('sysparm_zipcode');
//query the registration table
var output = false;
var ga = new GlideRecord('x_74571_kb_univ_ku_registration');
ga.addQuery('student_or_instructor', studentOrInstructor);
ga.addQuery('first_name', firstName);
ga.addQuery('last_name', lastName);
ga.addQuery('email', email);
ga.addQuery('phone_number', phoneNumber);
ga.addQuery('current_address', currentAddress);
ga.addQuery('city', city);
ga.addQuery('state', state);
ga.addQuery('zipcode', zipcode);
ga.query();
while(ga.next())
{
output = true;
}
return output.toString();
},
//Catalog Client Script
function onSubmit() {
var studentOrinstructor = g_form.getValue('student_or_instructor');
var fname = g_form.getValue('first_name');
var lname = g_form.getValue('last_name');
var email = g_form.getValue('email');
var phoneNumber = g_form.getValue('phone_number');
var currentAddress = g_form.getValue('current_address');
var city = g_form.getValue('city');
var state = g_form.getValue('state');
var zipcode = g_form.getValue('zipcode');
var gr = new GlideAjax('x_74571_kb_univ.SNO_AC_getAllFunctions');
gr.addParam('sysparm_name','getStudentRegistrationDetails');
gr.addParam('sysparm_student_or_instructor', studentOrinstructor);
gr.addParam('sysparm_fname', fname);
gr.addParam('sysparm_lname', lname);
gr.addParam('sysparm_email', email);
gr.addParam('sysparm_phoneNumber', phoneNumber);
gr.addParam('sysparm_currentAddress', currentAddress);
gr.addParam('sysparm_city', city);
gr.addParam('sysparm_state', state);
gr.addParam('sysparm_zipcode', zipcode);
gr.getXML(updateStudentRegistration);
//Response the function
function updateStudentRegistration(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'true') {
alert('Our database is showned you already registered as student. ');
g_form.clearValue('first_name', true);
g_form.clearValue('last_name', true);
g_form.clearValue('email', true);
g_form.clearValue('phone_number', true);
g_form.clearValue('current_address', true);
g_form.clearValue('city', true);
g_form.clearValue('state', true);
g_form.clearValue('zipcode', true);
}
else{
alert("No Record:"+answer);
}
}
}
Please Mark My Answer as Helpful and Accept as Solution, if you find this article helpful or resolves your issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-19-2024 02:28 PM
The code works. The alert pop up and clear the values. But i'm not able to stop the submit. From your experience, is there a way to clear the values and make the user can't submit this form? Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-19-2024 12:16 PM
@SnowDevOps would there be multiple record for single user in "x_74571_kb_univ_ku_registration" table ??
If there will be only record each user then you don't have to pass all the variables and apply that many filter conditions. Check what is the unique columns which identifies user record "x_74571_kb_univ_ku_registration" and pass only that variable value to script include for validation.
I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-19-2024 04:10 PM
Hi @SnowDevOps,
First of all, onSubmit does not work with AJAX. For a workaround, have a read at this article - https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0783579
A few things that I noticed:
- Is it possible to use a reference field for the 'student_or_instructor' variable? It looks like you are collecting a user's details on the form and if the data is already in ServiceNow (i.e. User table), there is no need to populate all these fields
- I don't understand why you need to check all these values in the custom table [x_74571_kb_univ_ku_registration]. My assumption is that you only need to verify if the user has already registered or not, and if so you just need a unique identifier of the user which could be the email or the phone number. There could be times when a user makes a typo in the address field, and if it happens, it will be counted as a new registerer
My advice is to revisit the logic before writing your script.
Cheers