The CreatorCon Call for Content is officially open! Get started here.

Hide a Widget based on the account of the logged in user

jabaya
Mega Guru

Hello gurus,

I need help with a Service Portal requirement. where i have a widget with a filter which i cloned and set filter of the cloned widget a little different, the challenged i have is to show / hide the widgets based on the logged is user.

the original widget will be the default one.

while the cloned widget will only show for the users of a certain account  ( example. account = xyz ).

i have tried to do a server script in the widget directly. ( tho im not knowledgeable enough to complete the script. -hence why i need help 🙂 ).

Attached is what I'm working on , any suggestions will be appreciated ❤️ Thankyou in advance 🙂


 

// Create a new GlideRecord object for the user table
var grUser = new GlideRecord('sys_user');

// Get the current logged-in user ID
var loggedInUser = gs.getUserID();

// Query the user record for the logged-in user
if (grUser.get(loggedInUser)) {
    // Check if the user account is 'urban utilities'
    if (grUser.u_account == 'XYZ') {
        gs.info('User account is XYZ');
        // Perform actions for the specific user account
	(-------------NOT SURE------------ what to add here to SHOW the widget)
    } else {
        gs.info('User account is not urban utilities');
        // Perform actions for other user accounts or handle accordingly
	(-------------NOT SURE------------ what to add here to HIDE the widget)
    }
} else {
    gs.info('Unable to find the logged-in user record');
}

 

 

6 REPLIES 6

Yashsvi
Kilo Sage

Hi @jabaya,

please check below code:

 

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; 

}

}

 

Thank you, please make helpful if you accept the solution.

This is based on Location, thanks for the idea. i also saw this in another thread. 

surajchacherkar
Mega Guru

Hi @jabaya ,

 

PFB link, seems like similar requirement.

 

https://www.servicenow.com/community/now-platform-forum/show-or-hide-widget-by-condition/m-p/984149 

 

If my response helped you, please click on "Accept as solution" and mark it as helpful.


Thanks

Suraj

jabaya
Mega Guru

made some progress with what im currently working on 
HTML

 

<div ng-if="data.isUrbanUtilities">
    <!-- Your widget content here -->
  
</div>

 




Server Script;

 

 

				(function() {
					data.isUrbanUtilities = false;

					// Get the current logged-in user object
					var user = gs.getUser();
					var userSysId = user.getID();
					gs.info('Logged-in User ID: ' + userSysId);  // Log the user ID for debugging

					// Use GlideRecord to get the user record
					var grUser = new GlideRecord('customer_contact');
					if (grUser.get(userSysId)) {
						var companyValue = grUser.getValue('company');
						gs.info('User account value: ' + accountValue);  // Log the account value for debugging

						// Check if the user account is 'urban utilities'
						if (companyValue == 'Urban Utilities') {
							data.isUrbanUtilities = true;
						}
					} else {
						gs.info('User record not found.');
					}

					gs.info('isUrbanUtilities: ' + data.isUrbanUtilities);  // Log the final result
				})();

 

 



however what i dont understand is that the widget now stays hidden even if the condition is met please see attached screenshot for the values im getting at based on the logs.