- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2019 01:07 AM
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!!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2019 02:01 AM
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;
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2019 01:43 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2019 01:49 AM
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:-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2019 02:00 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2019 02:01 AM
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;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2019 02:12 AM
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?