Widget for Service Portal to show Days Since Last P1 Incident
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2023 12:19 PM
Can anybody assist with this type of request? I have a requirement where they are wanting to show a "# of Days since Last P1 Incident" on our Service Portal for the organization to view. This is similar to factories that would display "so many days since an injury," etc. I found a topic that showed how to utilize a UI page and Widget under System UI, but not to display in the Service Portal. Any help would be appreciated.
The one topic I found is here: Days since - ServiceNow Community
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-03-2023 04:40 PM
I created a really simple Service Portal widget to test this out. Change it up to fit your needs though.
Body HTML template:
<div>
<!-- your widget template -->
<h3>Days since last P1</h3>
<h1 style="display: block;">{{data.days}}</h1>
</div>
Server script:
(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */
var gr = new GlideRecord('incident');
gr.addQuery('priority', 1);
gr.orderByDesc('sys_created_on');
gr.setLimit(1);
gr.query();
if(gr.next()){
var gdt = new GlideDateTime();
var today = gdt.getNumericValue();
var ldt = new GlideDateTime(gr.sys_created_on);
var last = ldt.getNumericValue();
var days = (today - last) / 1000 / 60 / 60 / 24;
data.days = Math.floor(days);
}
})();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-06-2023 10:34 AM
Hello Claude! This seems to have worked, but it won't show for our standard users. Any idea on how to make the count display regardless of role, etc?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-06-2023 12:16 PM
I'm sorry, but I'm not really certain on that piece. I would expect there is an ACL or Before Query Business Rule that would be preventing access to some or all records or specific fields in records. I also don't know your definitions of users so that's a very broad question. As it stands, the way the widget is set up is dynamic per the user's access to data.
I'm just guessing here, but let's say the standard users don't have access to any part of the incident table directly. This would cause the widget to fail providing the count in all cases. If they have access to some of the data, such as their own data, it is likely there is a record that is not theirs which is the last P1 so all but the one standard user would likely not have a count displayed. This could be corrected with a Before Query Business Rule for standard users that filters out all incidents that are not theirs to look at. This would result in the last time they had a P1.
From a global standpoint of communicating to users the last time there was a P1 in general, I'm really uncertain. I did think that maybe you could make use of a scheduled job to update the HTML in the widget via a Script Include. It's not something I've done before but is in the realm of possibilities. The data in the widget would be technically static, but it would be dynamically updated based on the daily update (or however often you want to have the scheduled job run.) You should be able to set to run those as admin as well based on what I'm seeing in my PDI.
I also searched for a solution for running as another user. I can see the benefit of doing this, but I'm more of the cautious type so I try to make sure actions are recorded as performed by the correct user. In any case, this is one solution I found: https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0820233