User criteria script

LucasEduarF
Tera Contributor

Hi, I am trying to create a user criteria to check the user's state using a script. I noticed that in sys_user there is a reference field called location.parent pointing to the cmn_location table. I don’t know how to do this dot walk. I also noticed that when opening the record in the cmn_location table, the field that contains the value I want is name.

 

this is the script i build: 

function checkCondition(){
    var user =  new GlideRecord('sys_user');
    user.get(gs.getUserID());
    if (user.location == 'PE') {
        return true;
    }
    return false;
}
6 REPLIES 6

YaswanthKurre
Giga Guru

HI @LucasEduarF ,

 

Please find the adjusted script:

function checkCondition() {
    var user = new GlideRecord('sys_user');
    if (user.get(gs.getUserID())) {
        // Dot-walk to location.parent.name
        var parentName = user.location.name.toString(); // for user location name  (user.location.parent.name;- if you want access location parent name)
        if (parentName == 'PE') { //the name of your location
            return true;
        }
    }
    return false;
}

 

Please mark this as helpful and correct if this helps you.

 

Thanks,

Yaswanth

J Siva
Tera Sage

Hi @LucasEduarF 
Try the below script in your user criteria record.

var user_id = gs.getUserID();
var user_rec = new GlideRecord('sys_user');
user_rec.get(user_id);
var loc = user_rec.location.name;
if (gs.nil(loc)){
	answer = false;
}else{
	if(loc == 'SHS quadra 5, Bloco E., Brasilia'){ //UPDATE THE NAME AS REQUIRED
		answer = true;
	}else{
		answer = false;
	}
}

Regards,
Siva

NagaChandaE
Tera Guru

hi @LucasEduarF 

answer = false;
var user_rec = new GlideRecord('sys_user');
user_rec.addQuery('sys_id',user_id);
user_rec.addQuery('location.name', 'PE');
user_rec.query()
if(user_rec.hasNext()){ 
	answer = true;
}

Ankur Bawiskar
Tera Patron
Tera Patron

@LucasEduarF 

try this

var user_rec = new GlideRecord('sys_user');
user_rec.addQuery('sys_id', user_id);
user_rec.addQuery('location.name', 'PE');
user_rec.query();
answer = user_rec.hasNext();

If my response helped please mark it correct and close the thread so that it benefits future readers.

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