How can we hide cart icon on header in service portals depending on company in ServiceNow

Ankita Gupte
Kilo Sage

We have cart icon on header menu of Service Portal, this can be hide if we set below value to false but I want it to hide only when logged in user is from company A. for logged in user from other company except A it should be visible.

 

{
"enable_cart": {
"displayValue": "true",
"value": false
},
"enable_wishlist": {
"displayValue": "true",
"value": false
}
}

Please advice how can we achieve this

10 REPLIES 10

Ahana 01
Tera Expert

To hide the cart icon on the header in Service Portals depending on the company in ServiceNow, you can follow these steps:

1. Navigate to the Service Portal Configuration.
2. Open the page where you want to hide the cart icon.
3. Find the widget that is responsible for displaying the cart icon. It's usually in the header widget.
4. Open the widget instance and navigate to the server script or client controller script.
5. In the script, you can add a condition to check the company of the logged-in user. You can use the GlideUser API to get the company of the current user. Here is a sample code:

javascript
var user = gs.getUser();
var company = user.getCompanyID();


6. Based on the company, you can decide whether to display the cart icon or not. Here is a sample code:

javascript
if (company == 'your_company_sys_id') {
data.showCartIcon = false;
} else {
data.showCartIcon = true;
}


7. Save the changes and refresh the portal to see the changes.

Please note that this is a high-level overview and the actual implementation might vary based on your ServiceNow version and configuration. Always test your changes in a non-production instance before applying them to production.


nowKB.com

For a good and optimistic result, and solving ServiceNow-related issues please visit this website.https://nowkb.com/home
Kindly mark correct and helpful if applicable

Hi Ahana,

I tried using above solution but its not working. I have pasted the code below. If you can take a look and let us know where to update it might help me. Thank you.

 

Server side code:

(function(){
// build menus
var menu_id = $sp.getValue('sys_id'); // instance sys_id
data.menu = {};
data.menu.items = $sp.getMenuItems(menu_id);
data.isLoggedIn = GlideSession.get().isLoggedIn();
data.guidedTours = {};
// Show tours only if gtd's sp sys property is set
data.showTours = (gs.getProperty('com.snc.guided_tours.sp.enable') === 'true') && data.isLoggedIn;
if (!data.showTours) {
data.guidedTours = null;
}
var catalogArr = ($sp.getCatalogs().value + "").split(",");
 
catalogArr.forEach(function(catalog) {
data.showWishlist = data.showWishlist || new sn_sc.Catalog(catalog).isWishlistEnabled();
});
if (data.isLoggedIn) {
if (options.enable_wishlist && data.showWishlist)
data.wishlistWidget = $sp.getWidget("sc_wishlist_cart", {wishlistTemplate: "small_wishlist.html", auto_update_wishlist:options.auto_update_wishlist});
if (options.enable_cart) {
if (gs.getProperty("glide.sc.portal.use_cart_v2_header", "false") === "true")
data.cartWidget = $sp.getWidget("sc-shopping-cart-v2", {cartTemplate: "small_shopping_cart_v2.html", auto_update_cart:options.auto_update_cart});
else
data.cartWidget = $sp.getWidget("sc-shopping-cart", {cartTemplate: "small_shopping_cart.html", auto_update_cart:options.auto_update_cart});
}
}
})();
 
Client controller code
function ($scope, spUtil, $rootScope, $timeout, spAriaUtil, spGtd, $window, i18n) {
$scope.collapse = function() {
$rootScope.$emit('sp-navbar-collapse');
}
$scope.loadingIndicator = $rootScope.loadingIndicator;
$scope.cartItemCount = 0;
$scope.wishlistItemCount = 0;
$scope.itemAddedTooltipOpen = false;
$scope.toursTooltipEnabled = true;
$scope.accessibilityEnabled = spAriaUtil.g_accessibility === "true";
$scope.$on("$sp.service_catalog.cart.count", function($evt, count) {
$scope.cartItemCount = count;
});
$scope.$on("$sp.service_catalog.wishlist.count", function($evt, count) {
$scope.wishlistItemCount = count;
});
var cancelTooltipPromise;
$scope.$on("$sp.service_catalog.cart.add_item", function() {
$timeout.cancel(cancelTooltipPromise);
$scope.itemAddedTooltipOpen = true;
cancelTooltipPromise = $timeout(function() {
$scope.itemAddedTooltipOpen = false;
}, 3000);
});
$scope.$on('sp_loading_indicator', function(e, value) {
$scope.loadingIndicator = value;
});
$scope.toggleCart = function() {
$timeout.cancel(cancelTooltipPromise);
$scope.itemAddedTooltipOpen = false;
$timeout(function() {
$("#cart-dropdown").dropdown("toggle");
});
};
$scope.toggleTours = function() {
var action = $scope.toursTooltipEnabled ? 'disable' : 'enable';
$scope.toursTooltipEnabled = $scope.toursTooltipEnabled ? false : true;
$('[data-toggle-second="tooltip"]').tooltip(action);
if(action == 'disable')
$('[data-toggle-second="tooltip"]').tooltip('hide');
}
$scope.onToursItemBlur = function() {
$('[data-toggle-second="tooltip"]').tooltip('enable');
$scope.toursTooltipEnabled = true;
}
// PRB1108244: visibleItems array is used to improve keyboard nav
// in menu, refresh it as needed
$scope.$watch('data.menu.items', function() {
$scope.visibleItems = [];
if ($scope.data.menu.items) {
for (var i in $scope.data.menu.items) {
var item = $scope.data.menu.items[i];
if (item.items || (item.scriptedItems && item.scriptedItems.count != 0))
$scope.visibleItems.push(item);
}
}
}, true);
$scope.$on('sp-menu-update-tours', function(event, tours) {
$scope.data.showTours = $scope.data.showTours && !spUtil.isMobile();
if ($scope.data.showTours === false) {
$scope.data.guidedTours = null;
return;
}
var guidedToursLabel = 'Guided Tours';
$scope.data.guidedTours = {
label: guidedToursLabel,
collection: []
};
$scope.tooltipTours = tours.length === 1 ? i18n.getMessage('This page currently has 1 tour') : i18n.getMessage('This page currently has {0} tours').withValues([tours.length])
if (tours.length > 0) {
$scope.data.guidedTours.collection = tours.map(function(t) {
return {
title: t.name,
id: t.id,
clicked: function() {
spGtd.launch(t.id);
}
};
});
}
});
// Get list of record watchers
var record_watchers = [];
if ($scope.data.menu.items) {
for(var i in $scope.data.menu.items) {
var item = $scope.data.menu.items[i];
if (item.type == 'scripted') {
if (item.scriptedItems.record_watchers)
record_watchers = record_watchers.concat(item.scriptedItems.record_watchers);
}
if (item.type == 'filtered') {
record_watchers.push({'table':item.table,'filter':item.filter});
}
}
}
// Init record watchers
for (var y in record_watchers){
var watcher = record_watchers[y];
spUtil.recordWatch($scope, watcher.table, watcher.filter);
}
$rootScope.$broadcast('sp-header-loaded');
}

Dr Atul G- LNG
Tera Patron
Tera Patron

Are you using domain separation?

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

no not domain separation