How to define user criteria with part of email address from sys_user

thomast123
Kilo Contributor

Hi. 

Im trying to create a user criteria for knowledge base.

The idea is to checking if the sys_user record and the field 'emai'l contains  "@userdomain.com" returns to true then the user with email that have "@userdomain.com" should be granted access to the knowledge base with the defined user criteria.

if a users email contains for example "@wrong.userdomain.com" then it should be not granted any access to the knowledge base.

I've tried to put together a script, but the outcome is that any knowledge base that have the user criteria added to can read list is then available for all users, even the unauthenticated ones...

Script : 

var user = new GlideRecord('sys_user');
var email = user.email;

if(email.indexOf("@userdomain.com") != -1){
answer = true;
}
else{
answer = false;
}

Is my script completely off?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

as best practice always use user_id to get logged in user sys_id

updated script

var user = new GlideRecord('sys_user');
if(user.get(user_id)){
	var email = user.email.toString();

	if(email.indexOf("@userdomain.com") != -1){
		answer = true;
	}
	else{
		answer = false;
	}
}

Regards
Ankur

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

View solution in original post

4 REPLIES 4

Jaspal Singh
Mega Patron
Mega Patron

Try below

var usrid=gs.getUserID();
var useris = new GlideRecord('sys_user');
useris.addQuery('sys_id',usrid);
useris.query();
if(useris.next())
{
var emailis = useris.email;
if(emailis.indexOf("@userdomain.com") > -1){
answer = true; //show
}
else{
answer = false; //hide
}
}

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

as best practice always use user_id to get logged in user sys_id

updated script

var user = new GlideRecord('sys_user');
if(user.get(user_id)){
	var email = user.email.toString();

	if(email.indexOf("@userdomain.com") != -1){
		answer = true;
	}
	else{
		answer = false;
	}
}

Regards
Ankur

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

Thank you so much @Ankur Bawiskar  this solved the problem 🙂

Hello Ankur,

 

my script below for user criteria is not working. Can you help me?

 

 
        var gr = new GlideRecord('sys_user');
        if (gr.get(user_id)) {
         var userEmail = gr.email.toString();
          if (userEmail.indexOf('extern') != -1) {
          answer = true;
          } else {
          answer = false;
          }