How to check user has any roles from any group?.

Sai25
Giga Guru

Hi,

After group approval it goes for the license team approval. if user did not have same roles.

example :adela.cervantsz  has role scrum_user and when we are adding any group need to check user has roles not only one role any roles need to check from any group.

If found same roles need to bypass it to next activity and if did not found need to go for license team approval. So, license team will add the roles.

Below script i used to check the same, but not working.

answer = ifScript();
function ifScript() {
var x=current.variables.select_name;
x=gs.getUserID();
var y=current.variables.select_group;

var ge =new GlideRecord('sys_user_has_role');
ge.addQuery('user',x);
ge.query();
while(ge.next())
{
var gr=new GlideRecord('sys_group_has_role');
gr.addQuery('group',y);
gr.addQuery('role',ge.sys_id);
gr.query();
if(gr.next()) {
return 'yes';
}
}
return 'no';
}

below is the workflow.

find_real_file.png

Could you please help me.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sai,

Can you explain with an example?

Is the script not working as expected?

Are you saying the select_name variable has some user

if that user has many roles say A,B,C

and now you want to check if the group selected in select_group variable has any of those role then return yes else return no

why you are using gs.getUserID() if you want to check role for select_name user?

update code as below

also ge.role will give you role sys_id while comparing in sys_group_has_role

answer = ifScript();
function ifScript() {
var x = current.variables.select_name; // I assume select_name is a reference variable

// x=gs.getUserID();

var y=current.variables.select_group;

var ge =new GlideRecord('sys_user_has_role');
ge.addQuery('user',x);
ge.query();
while(ge.next())
{
var gr=new GlideRecord('sys_group_has_role');
gr.addQuery('group',y);
gr.addQuery('role',ge.role);
gr.query();
if(gr.next()) {
return 'yes';
}
}
return 'no';
}

Regards

Ankur

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

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sai,

Can you explain with an example?

Is the script not working as expected?

Are you saying the select_name variable has some user

if that user has many roles say A,B,C

and now you want to check if the group selected in select_group variable has any of those role then return yes else return no

why you are using gs.getUserID() if you want to check role for select_name user?

update code as below

also ge.role will give you role sys_id while comparing in sys_group_has_role

answer = ifScript();
function ifScript() {
var x = current.variables.select_name; // I assume select_name is a reference variable

// x=gs.getUserID();

var y=current.variables.select_group;

var ge =new GlideRecord('sys_user_has_role');
ge.addQuery('user',x);
ge.query();
while(ge.next())
{
var gr=new GlideRecord('sys_group_has_role');
gr.addQuery('group',y);
gr.addQuery('role',ge.role);
gr.query();
if(gr.next()) {
return 'yes';
}
}
return 'no';
}

Regards

Ankur

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

Hi Ankur,

You are correct if that user has many roles say A,B,C. when we are adding any group same roles are there in that group need to return as yes. If the roles are not matching need to send it for license team approval.

 

 

Thanks,

Sai.

Hello Ankur,

i have to set the redirection rule for users having particular role to Service Portal and CSM portal, However i am facing issues with the SPentry page script:

I have the below issues:

  1. User if remove the "/sp" or "/csm" manually then it should redirect to Platform, it always redirects to portal on the condition
  2. For the declaration below:   var isExternal = (!user.hasRole('admin') && !user.hasRole('snc_internal')) && user.hasRole('snc_external');  if i assign any other role for a user with "snc_external" role then still the user is routed to CSM portal and not platform, how can i query users with having these roles exactly
  3. for the users added to the stakeholder group only, they should redirect to Service portal, if they are added to any other group having roles then they should route to the platform which is not happening.

Below is the Script for SPentryPage:

var SPEntryPage = Class.create();

SPEntryPage.prototype = {

initialize: function() {
this.logVariables = false; // for debugging
this.portal = "/sp/"; // The URL suffix specified in the sp_portal record
this.kbPortal = "/kb/";
this.csmPortal = "/csm/";
},

/***
*
* Referred to by property: glide.entry.page.script
*
**/
getLoginURL: function() {
// When requesting a page directly (eg: /problem_list.do)
// The platform session_timeout.do sets the login page to welcome.do
// Since we are handling the login, clear that value
var session = gs.getSession();
var nt = session.getProperty("nav_to");
var sPage = session.getProperty("starting_page");
if (nt == "welcome.do")
session.clearProperty("nav_to");

if (!sPage && !nt)
session.putProperty("starting_page", gs.getProperty("glide.login.home"));

// Send the user to the portal's login page
var portalGR = new GlideRecord("sp_portal");
portalGR.addQuery("url_suffix", this.portal.replace(/\//g, ""));
portalGR.addNotNullQuery("login_page");
portalGR.query();
if (portalGR.next())
return this.portal + "?id=" + portalGR.login_page.id;

// Send to the a default login page
return this.portal + "?id=login";
},

/***
*
* Referred to by property: glide.entry.first.page.script
*
**/
getFirstPageURL: function() {
var session = gs.getSession();
this.logProperties('before', session);

// has roles and is not a Service Portal page - go to UI16
var nt = session.getProperty("nav_to");
var isServicePortalURL = new GlideSPScriptable().isServicePortalURL(nt);
var redirectURL = session.getProperty("login_redirect");

//if added to one group should redirect, but how do i redirect to platform if member of additional groups?
if(user.isMemberOf('Stakeholder Group'))
return this.portal;

//if additional roles added to a user, it still redirects to CSM portal only
var isExternal = (!user.hasRole('admin') && !user.hasRole('snc_internal')) && user.hasRole('snc_external');
// my code here below
var isInternal = user.hasRole('snc_internal');

//if i add manually the roles, it works, but if i add another role, it does not redirect to Platform and the condition is still matched
var isStakeholder = (!user.hasRole('admin')) && user.hasRole('snc_internal') && user.hasRole('approver_user');

if(isInternal && redirectURL){
return this.portal;
}
if(isStakeholder && !redirectURL){
return this.portal;
}

if (isExternal && !redirectURL) {
return this.csmPortal; 
}

if (user.hasRoles() && !redirectURL && !isServicePortalURL)
return;

// user may have logged in from a frame, the /login_redirect.do page will bust out of it
if (!redirectURL) {
// redirectURL is nav_to
// if nav_to == "welcome.do" then use starting_page
var sPage = session.getProperty("starting_page");
if (sPage && nt == "welcome.do")
nt = sPage;

// Avoid a redirect loop to the home page
var ep = gs.getProperty("glide.login.home");
if (nt) {
if (ep == nt)
nt = null;
}
// PRB726860: if page is still welcome.do, go to glide.login.home preserving frameset
if (nt == "welcome.do") {
session.putProperty("nav_to", ep);
return;
}

session.putProperty("login_redirect", nt || "true");
return "/login_redirect.do?sysparm_stack=no";
}

session.clearProperty("login_redirect");
var returnUrl = this.kbPortal;
if (redirectURL && redirectURL != "true") {
var spUrl = new GlideSPScriptable().mapUrlToSPUrl(redirectURL);
returnUrl = spUrl ? this.portal + "?" + spUrl : redirectURL;
}

this.logProperties('after', session);
if (!this.logVariables) {
gs.log('redirectURL: ' + redirectURL);
gs.log('User: ' + user.getName());
gs.log('is internal: ' + (!user.hasRoles()));
gs.log('returnUrl: ' + returnUrl);
}

return returnUrl;
},

logProperties: function(beforeOrAfter, session) {
if (!this.logVariables)
return;

gs.log('SPEntryPage: Redirect ------------------------------- ' + beforeOrAfter);
gs.log('session.starting_page: ' + session.getProperty("starting_page"));
gs.log('session.nav_to: ' + session.getProperty("nav_to"));
gs.log('session.login_redirect: ' + session.getProperty("login_redirect"));
gs.log('gs.fURI: ' + session.getURI());
},

type: 'SPEntryPage'
};

try to debug by adding gs.info() statements

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