If condition does not contain is not working

AnilM99
Tera Expert

Hi Team

I am using workflow run script to check email variable,

If users enters name instead of @sgis.com  then it should check user table else it should continue.

 

Background Script:
var current = new GlideRecord("sc_req_item");
current.get("71793a9397147110d0d9f0571153af9a");
gs.print(current.variables.email);
if (current.variables.email == ""){
current.variables.email = "anil@sgis.com"
}
 
else if(current.variables.email.indexOf("@sgis.com") == -1 ){
gs.print("Hello not @sgis.com");
var getEmail = new GlideRecord("sys_user");
getEmial.addActiveQuery();
getEmail.addQuery('name', current.variables.email);
getEmail.query();
if(getEmail.next()){    
gs.print("Hello User Found");
current.variables.email = getEmail.email;
 
}else{
gs.print("Hellow User Not Found");
current.variables.email = "anil@sgis.com";
 
}
}
 
Help me on the same
 
Thanks Anil
4 REPLIES 4

Bert_c1
Kilo Patron

Hi @AnilM99,

 

If you want to query the sys_user table using an email address, then change:

 

getEmail.addQuery('name', current.variables.email);

to

getEmail.addQuery('email', current.variables.email);

 

As the 'name' field on sys_user table is not an email address.

Hi @Bert_c1 

thanks for the reply,

my query is if user enter name instead of email, we are checking name in user table and get the user email address.

If user enter email address correctly (eg. anil@sgis.com) we are not checking user table continue the workflow next step

 

Thanks,

 Anil

hello there is a error in your script. Also donot use current as a variable in script to gliderecord replace that with ritm or define some other variable

 

var current = new GlideRecord("sc_req_item");
current.get("71793a9397147110d0d9f0571153af9a");
gs.print(current.variables.email);
if (current.variables.email == ""){
current.variables.email = "anil@sgis.com"
}

else if(current.variables.email.indexOf("@sgis.com") == -1 ){
gs.print("Hello not @sgis.com");
var getEmail = new GlideRecord("sys_user");
getEmial.addActiveQuery(); //getEmail not getemial
getEmail.addQuery('name', current.variables.email); //email is a field where user enters name and email both?
getEmail.query();
if(getEmail.next()){
gs.print("Hello User Found");
current.variables.email = getEmail.email;

}else{
gs.print("Hellow User Not Found");
current.variables.email = "anil@sgis.com";

}
}

Regards
Harish

Sagar Pagar
Tera Patron

Hi @AnilM99,

 

I would suggest to use the Email id validation for email variable. it will avoid the issues, if user enters name.

 

Updated scripts:

var current = new GlideRecord("sc_req_item");
current.get("71793a9397147110d0d9f0571153af9a");
gs.print(current.variables.email);

if (current.variables.email == "") {
	current.variables.email = "anil@sgis.com";
	
} else if (current.variables.email.toString().indexOf("@sgis.com") == -1) {
	gs.print("Hello not @sgis.com");
	
	var getEmail = new GlideRecord("sys_user");
	getEmial.addQuery('active=true');
	getEmail.addQuery('name', current.variables.email);
	getEmail.query();
	if (getEmail.next()) {
		gs.print("Hello User Found");
		current.variables.email = getEmail.email.toString();

	} else {
		gs.print("Hellow User Not Found");
		current.variables.email = "anil@sgis.com";

	}
}

 

If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers.
Thanks,
Sagar Pagar

The world works with ServiceNow