Hide widget based on Region of logged in user

kun1
Tera Expert

Hi Team, 

We have to hide the widget or page based on the region code of logged in user. To achieve this, i have created a User criteria and write a script in it but its not working.

var locationId = gs.getUserID().getLocation();
var usrLocation = new GlideRecord('cmn_location');
usrLocation.addQuery('sys_id', locationId);
usrLocation.query();
gs.log("Row count=" + usrLocation.getRowCount());
while (usrLocation.next())
{
var userRegion = usrLocation.getValue('u_gyregion_code');
gs.log("userRegion=" + userRegion);
if(userRegion == "NA")
{
answer = true;
}
else {
answer = false;
}
}

Please take a look and help me to correct this.

Thanks!!

1 ACCEPTED SOLUTION

try now. 

 

checkCondition();

function checkCondition(){

var locationId = gs.getUser().getLocation();

var usrLocation = new GlideRecord('cmn_location');
usrLocation.addQuery('sys_id', locationId);
usrLocation.query();
if(usrLocation.next())
{
var userRegion = usrLocation.getValue('u_gyregion_code');

if(userRegion == "NA") 
{
return true;
}
return false; 

}

}

View solution in original post

14 REPLIES 14

u_gyregion_code is reference type field ? if yes then add var userRegion = usrLocation.getDisplayValue('u_gyregion_code');

 

i am assuming here you have cloned "announcement" widget and set the correct widget in your service portal user criteria

 

still now working , kindly add the screenshot. 

First of all, Region code is not a reference type field, it a string.

and i am added the screenshot here of the widget, portal:-

 

Hi harsh,

In the can view , i added this user criteria and impersonate with the end user whose region code is NA   but its hide for the all the users who doesn't have admin role

try now. 

 

checkCondition();

function checkCondition(){

var locationId = gs.getUser().getLocation();

var usrLocation = new GlideRecord('cmn_location');
usrLocation.addQuery('sys_id', locationId);
usrLocation.query();
if(usrLocation.next())
{
var userRegion = usrLocation.getValue('u_gyregion_code');

if(userRegion == "NA") 
{
return true;
}
return false; 

}

}

I tried with this and its working fine but if the user having admin role and from diff region code other than NA then he also able to see that widget.

and why we use function here?

and if use while instead of if, it will create problem here?