OnChange Not Taken into Account OnSubmit

A_Nelson
Kilo Expert

Oh so, I have a form to create a new user. When creating, I have an onChange client script that calls to a script include when a user enters the email to take a look at the existing users to make sure we do not already have a user with the same email in ServiceNow (see below). 

 

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	var ga = new GlideAjax('getUserObjects');
	ga.addParam('sysparm_name', 'compareEmail');
	ga.addParam('sysparm_email', newValue);
	ga.getXML(EmailCompareParse);
	
}

function EmailCompareParse(response) {
	
	var answer = response.responseXML.documentElement.getAttribute("answer");
	if (answer == 'true') {
		g_form.clearValue('nars_email');
		alert('A user with this email address already exists. Please select "Modify User" from the home page to change an existing user\'s information, center, or access.' );
	}
	
}

 

This works perfectly fine if the last variable they update is NOT the email field. In some cases though, the last option they are presented is the email field and if they do not click off of the field or hit tab to kick off the onChange script, it will still allow them to submit the form but will still cleat the value in the email field and will show the alert.

I attempted doing the same script as an onSubmit (see below) but it will not work. I have run into asynchronous issues with client scripts before and I am assuming this might be the same problem. Any idea how to get around this??

 

function onSubmit() {
	
	var email = g_form.getValue('nars_email');
	
	var ga = new GlideAjax('getUserObjects');
	ga.addParam('sysparm_name', 'compareEmail');
	ga.addParam('sysparm_email', email);
	ga.getXML(EmailCompareParse);
	
}

function EmailCompareParse(response) {
	
	var answer = response.responseXML.documentElement.getAttribute("answer");
	if (answer == 'true') {

		return false;
	}	
	else{
  	
		alert('Thank you for your submission. Instructions for logging on will be sent to the user once your request has been completed.');
	}
}

 

Many thanks,

1 ACCEPTED SOLUTION

A_Nelson
Kilo Expert

 

Did some more digging and found an article with some code that I manipulated to get this working.

Asynchronous onSubmit Catalog/Client Scripts in ServiceNow

https://snprotips.com/blog/2018/10/19/synchronous-lite-onsubmit-catalogclient-scripts

 

Although the content of the article states that the code is to work around not using a hidden variable on the form, I went ahead and did jist that. I could not get the code for the get/set/putClientData to send back anything but 'undefined' for some reason and using a hidden checkbox field is working perfectly now with the below script.

I also tried to do this with my GlideAjax calling to my script include, however I was also unable to get that working. Since I am working with the sys_user table though, this code worked as a great example.

function onSubmit() {
	
	// Hide any existing messages from previous validation attempts
    g_form.clearMessages();
	
	// Gather the Variables
	var email = g_form.getValue('nars_email');
	var user_validation = g_form.getValue('user_validation');
	
	// If the user was validated, stop here and submit the form.
    if (user_validation == 'true') { 
		
		// Return true to allow submission
        return true;
    } 
	
	//If the user has not yet been validated, do so
    var grUser = new GlideRecord('sys_user');
		grUser.addQuery('email', email);
		grUser.setLimit(1);
	
		//Query and pass in the callback function
		grUser.query(validateUser);
	
    // Return false to prevent submission (for now)
    return false;
}

function validateUser(grUser) {
	
    // If a user was found with the entered email,
	if (grUser.next()) {
		
		// Set the hidden variable to false
		g_form.setValue('user_validation', false);
        
    } 
	else { 
		
		//If a user was not found with the enetered email, set hidden variable to true
		g_form.setValue('user_validation', true);
		
		// Alert the user of the submission
		alert('Thank you for your submission. Instructions for logging on will be sent to the user once your request has been completed.');
		
        // Re-submit the form
        g_form.submit(); //(This will trigger line 14 above, and submit the form as the user is now validated)      
    }
}

View solution in original post

5 REPLIES 5

Glad you got it resolved. Please mark any other reply as Helpful if it was and thanks for posting your resolution.

Take care!


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