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

Alikutty A
Tera Sage

Hi,

Can you try this script and see if it works?

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

Thanks!

 We have a widget announcement which we want to hide for the NA Region users. So , in the widget we added this user criteria under the cannot view. This widget should be hidden but it but it still showing on the portal..

Harsh Vardhan
Giga Patron

change the first line. 

it should be 

 

gs.getUser().getLocation()

 

var locationId = gs.getUser().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; 
}
}

Yes Harsh, i already changed the first line of the code and run the code in the background script and it showing the output as expected. We have a widget announcement which we want to hide for the NA Region users. So , in the widget we added this user criteria under the cannot view. This widget should be hidden but it still showing on the portal.