Pop-up code works in Xanadu but broken in Yokahama
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago - last edited 4 weeks ago
Hi folks,
I have the following script in an OnLoad / OnUpdate business rule to enforce a default dashboard for itil users. The code sets a session property so that if they navigate to a different dashboard, they are prompted that the default will be set for the next time they log into ServiceNow. The pop-up only happens once per session. This works great in versions up to and including Xanadu but we are testing Yokahama and this seems to be broken in this release. In Yokahama, the pop-up happens as soon as the page loads, rather than when they navigate to a new dashboard. Does anyone know what may have changed and can maybe suggest a fix?
(function executeRule(current, previous /*null when async*/) {
var itilRoleSysId = 'MYITILROLESYSIDHERE'; // Sys ID for ITIL role
var requiredDashboardValue = '{"last":"MYREQUIREDDASHBOARDSYSIDHERE"}'; // Desired dashboard value
// Retrieve the session object
var session = gs.getSession();
// Check if messageCount is not set and initialize it to 0
var messageCount = session.getProperty('messageCount');
if (messageCount === null || messageCount === undefined) {
messageCount = 0;
session.putProperty('messageCount', messageCount);
} else {
messageCount = parseInt(messageCount, 10); // Ensure it's treated as an integer
}
// Check if the preference being changed is the dashboard preference
if (current.name == 'com.snc.pa.ui.preferences_dashboards') {
var user = new GlideRecord('sys_user');
if (user.get(current.user)) {
// Check if the user has the ITIL role
var hasITILRole = new GlideRecord('sys_user_has_role');
hasITILRole.addQuery('user', user.sys_id);
hasITILRole.addQuery('role', itilRoleSysId);
hasITILRole.query();
if (hasITILRole.next()) {
// If the preference is not set to the required value, enforce it
if (current.value != requiredDashboardValue) {
current.value = requiredDashboardValue;
// Only display the message if messageCount is 0
if (messageCount === 0) {
gs.addInfoMessage('The KPI dashboard will be set for your next login.');
messageCount++; // Increment the messageCount
session.putProperty('messageCount', messageCount); // Update the session variable
}
}
}
}
}
})(current, previous);
I thought about having the pop-up logic moved to a UI script and attach it to the home page but I can't figure out the attaching part.
As always, any help is appreciated.
Thanks,
Ken
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
this is the display business rule?
share the business rule config screenshots.
Also where is the logic for pop-up? share that component and also screenshots for the same.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hi Ankur,
Thanks for your reply:
The pop-up logic is in the same script:
// Retrieve the session object
var session = gs.getSession();
// Check if messageCount is not set and initialize it to 0
var messageCount = session.getProperty('messageCount');
if (messageCount === null || messageCount === undefined) {
messageCount = 0;
session.putProperty('messageCount', messageCount);
} else {
messageCount = parseInt(messageCount, 10); // Ensure it's treated as an integer
}
//...
// Only display the message if messageCount is 0
if (messageCount === 0) {
gs.addInfoMessage('The KPI dashboard will be set for your next login.');
messageCount++; // Increment the messageCount
session.putProperty('messageCount', messageCount); // Update the session variable
}
Thank you,
Ken