Email validation on Multi-row variable sets

DB1
Tera Contributor

Hi All,

 

I am looking for solution help with the below requirement.

 

1. I have a multi-row variable set with variables : "Name", "Title", "Phone", "Email" etc.

2. I want to have the validation on "Email".

Detail: If the User enters an email address it has to check the "sys_user" database to see if it returns any entry from the User record. If yes, it has to throw an error "User found!"

Can we do the validation on each row of the Multi- row variable when "Add" is selected every time?

 

DB1_0-1696851310887.png

 

 

I need help to build a solution for the above requirement.

 

TIA,

DB

 

@Peter Bodelier @Ankur Bawiskar  @AnveshKumar M 

 

 

 

 

11 REPLIES 11

Ashwini Jadhao
Giga Guru

Hi @DB1 ,

Create on change client script on Email variable and add below code:

 var usr = new GlideRecord('sys_user');
    usr.addQuery('email', g_form.getValue('test_text'));//add email variable name from variable set
    usr.query();
    if (usr.next()) {
        alert("user Found");
    } else {
        alert("user not Found");
    }

Vishal Birajdar
Giga Sage

Hi @DB1 

 

You can write script include & onChange Catalog client script written on MRVS

 

Client callable Script include :

Name : userUtils

 

 

 

checkUserEmail: function() {

        var isEmailPresent;
        /* Get email from MRVS email field */
        var email = this.getParameter('sysparm_email');

        /*  glide record on user table and check email address - hoping you are checking it on user table*/
        var grUser = new GlideRecord('sys_user');
        grUser.addQuery('email', email);
        grUser.query();

        if (grUser.next()) {
            isEmailPresent = "true";
        } else {
			isEmailPresent = "false";
		}
		return isEmailPresent;
    },

 

 

 

Client script :

 

 

 

function onChange(control, oldValue, newValue, isLoading) {

	if(isLoading){
     return;
	}
    var email = g_form.getValue('email');

    var ga = new GlideAjax('userUtils');  // scripot include name
    ga.addParam('sysparm_name', 'checkUserEmail');  // function name
    ga.addParam('sysparm_email', email);

    ga.getXMLAnswer(callBackFun);


    function callBackFun(answer) {
      
		if(answer == 'true'){
               g_form.showErrorBox('email', "Email id already present");
			
		}else {
			 g_form.showFieldMsg('email', "You can proceed with this email");
		}

    }

}

 

 

 

Make sure that Client script written on your MRVS :

 

VishalBirajdar_1-1696854563125.png

 

 

 

Output :

 

 

Email already exist :

VishalBirajdar_0-1696854532897.png

 

New email : 

 

VishalBirajdar_2-1696854638360.png

 

Additional information : 

You can set "g_scratchpad" value depending upon result in client script & use that g_scratchpad value in onSubmit script to allow (if new email) or abort the submission if (email already exist)

 

Hope this helps...!!

  

 

 

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Ankur Bawiskar
Tera Patron
Tera Patron

@DB1 

What did you start with? It should be an easy task

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

DB1
Tera Contributor

Hi All, Thanks for your replies. I was not available until today.

 

@Ankur Bawiskar @Vishal Birajdar 

 

I have the script include and client script as below

 

SI:

checkUserEmail: function() {

		var isEmailPresent;
		/* Get email from MRVS email field */
		var email = this.getParameter('sysparm_email');

		/*  glide record on user table and check email address - hoping you are checking it on user table*/
		var grUser = new GlideRecord('sys_user');
		grUser.addQuery('email', email);
		grUser.query();

		if (grUser.next()) {
			isEmailPresent = "true";
		} else {
			isEmailPresent = "false";
		}
		return isEmailPresent;
	},

client:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	var email = g_form.getValue('abc_external_email');

	var ga = new GlideAjax('ABC_checkUserEmail');  // script include name
	ga.addParam('sysparm_name', 'checkUserEmail');  // function name
	ga.addParam('sysparm_email', email);

	ga.getXMLAnswer(callBackFun);


	function callBackFun(answer) {

		if(answer == "true"){
			//g_form.showErrorBox('crf_external_email', "Email id already present");
			return true;

		}
		else 
	
		{
			g_form.showErrorBox(email, "Email Id does not exist");
			return false;
		}

	}
}

I have applied it to the variable set only. still I do not see any error message.

I want the error message only when email id is not found on sys_user table. If found it should not do anything just return true