Validate the form using script include

SnowDevOps
Giga Guru

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

	}
   }

}
1 ACCEPTED SOLUTION

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

}

 

View solution in original post

11 REPLIES 11

I found something very interesting. If you test with a different user who is never been registered on the table. I got an alert for "no record:false". It wouldn't allow you to register it. It prevents you to click on the submit button. I feel like our code is upside down here. If the user's information  is matched, it shouldn't allow him/her register and it should prevent you to click on the submit button. If it is not match, it should allows it goes through. 

I found something very interesting. If you test with a different user who is never been registered on the table. I got an alert for "no record:false". It wouldn't allow you to register it. It prevents you to click on the submit button. I feel like our code is upside down here. If the user's information  is matched, it shouldn't allow him/her register and it should prevent you to click on the submit button. If it is not match, it should allows it goes through.