- 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 08:33 PM
Hello
Thanks for the workaround. How do you use on the catalog client script? I'm not familiar with Scratchpad. I don't even know how to modify my current client script for this workaround.
Yeah, that was my rookie's mistake. I used the custom table instead of the sys_user table at the beginning of this project. It seems to make things harder rather than easier. I learned my lesson not to do it again for the next project. 😀
The reason why i checked all these values is because I want to make sure if they make the mistake on either one or more of these fields, you have other fields left to validate.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2024 10:22 PM
No worries, update your client script as below. Note that I removed parts of your script for legibility only.
function onSubmit() {
if (g_scratchpad.isFormValid) {
return true;
}
/**Your script here...
//Get the value in each of variables that you want from the form field
var studentOrinstructor = g_form.getValue('student_or_instructor');
......
*/
//Response the function
function updateStudentRegistration(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'true') {
/**Your script here...
alert('Our database is showned you already registered as student. ');
.....
*/
g_scratchpad.isFormValid = true;
g_form.submit(g_form.getActionName());
} else {
alert("No Record:" + answer);
}
}
}
One caveat I realized is that this does not work in the backend/Native UI.
If you are submitting the catalog item in the portal only, it should be fine.
In terms of 'checking' all the values, you are using the 'AND' operator for all the fields, meaning all of the values need to match. So, if the user makes one single typo, it will be treated as a new registerer.
My understanding is that you want to check if the user is already registered or not. If so, you will need to use a unique identifier of the user to check if the user is in the system or not.
In your example, the closest identifier would be the email address and/or the phone number (But of course, these are not unique as you can change your number/email address). Once you figure out what the unique identifier will be, you can simply query with that particular field without needing to check all the fields (address, name, etc)
- 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-21-2024 12:15 AM
Ah apologies, I missed a line of script.
Add the following line right underneath the gr.getXML(updateStudentRegistration);
return false;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2024 11:30 PM - edited 04-21-2024 11:33 PM
Yes, It works now. It's interesting how this workaround works that ServiceNow gave us. When you hit the submit button, the request goes through but it doesn't add the user into the table. i guess this is how this workaround works. This maybe the best workaround for onSubmit that we can have.
Thank you for your help ! Appreciated so much ! 👍