if condition is not working in script include

Anusha Anus
Tera Expert

Hello,

 

I am calling script include from reference qualifier. If requested for is logged in user and he is not a manager and not having ITIL role, then he should able to see only his record in Requested for variable.

 

if (grusr.manager_flag == false) {
var userRole = new GlideRecord('sys_user_has_role');
userRole.addEncodedQuery('user.u_code=1^role!=282bf1fac6112285017366cb5f867469^user=' + myuser);
userRole.query();

gs.log("user value" + userRole.sys_id.toString());
return userRole.sys_id.toString();

}

 

Here log message returning one sys id(logged in user). But in the Requested for variable he can able to see all the records.

 

He should able to see only one record.

 

Can you please help on it.

@Anil Lande 

 

Thanks,

Anus

 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

You should always use the .next() method with GlideRecord - if (userRole.next()) to process only one record, while (userRole.next()) when more than one query result is expected.  In your case, since you are only expecting one result with such as specific encoded query, you'll use if(... Your reference qualifier is defaulting to showing all records because it is invalid.  You are returning the sys_id of the sys_user_has_role table record, not the user sys_id, so that's not matching a user sys_id on the requested_for (sys_user) table, and the format is not correct - depending on what you are using in the reference qualifier.  Try this:

 

if (grusr.manager_flag == false) {
    var userRole = new GlideRecord('sys_user_has_role');
    userRole.addEncodedQuery('user.u_code=1^role!=282bf1fac6112285017366cb5f867469^user=' + myuser);
    userRole.query();
    if (userRole.next()) {
        gs.log("user value" + userRole.user.toString());
        return 'sys_idIN' + userRole.user.toString());
    }
}

 

 

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

You should always use the .next() method with GlideRecord - if (userRole.next()) to process only one record, while (userRole.next()) when more than one query result is expected.  In your case, since you are only expecting one result with such as specific encoded query, you'll use if(... Your reference qualifier is defaulting to showing all records because it is invalid.  You are returning the sys_id of the sys_user_has_role table record, not the user sys_id, so that's not matching a user sys_id on the requested_for (sys_user) table, and the format is not correct - depending on what you are using in the reference qualifier.  Try this:

 

if (grusr.manager_flag == false) {
    var userRole = new GlideRecord('sys_user_has_role');
    userRole.addEncodedQuery('user.u_code=1^role!=282bf1fac6112285017366cb5f867469^user=' + myuser);
    userRole.query();
    if (userRole.next()) {
        gs.log("user value" + userRole.user.toString());
        return 'sys_idIN' + userRole.user.toString());
    }
}

 

 

James Chun
Kilo Patron

Hey @Anusha Anus ,

 

The 'requested_for' field references the User table [sys_user] and within your script, you are returning a sys_id of a User Roles [sys_user_has_role] record. 

Changing the return statement to something like below should fix it:

return userRole.getValue('user');

 

Thanks!