Skip to content

Commit 04c6cbd

Browse files
Share reports with users and groups (#1905)
* Create Code.js * Create README.md * Add Business rule: Add or remove tag to the ticket * Rename code.js to code.js * Rename README.md to README.md * Adding new UI Action: Generate PDF * Adding new UI Action: Generate PDF * Adding new Background script: Bulk Update of Fulfillment Group References in Published KB Articles * Adding new Background script: Bulk Update of Fulfillment Group References in Published KB Articles * Delete Server-Side Components/Background Scripts/Bulk Update of Fulfillment Group References in Published KB Articles directory * Update Script.js * Update README.md * Update README.md * Delete Client-Side Components/UI Actions/Generate PDF directory * Create script.js * Create README.md * Create script.js * Create README.md * Delete Server-Side Components/Script Includes/Calculate Due date using user defined schedules directory * Create README.md * Add files via upload * Update script.js * Update README.md * Create README.md * Update README.md * Create ui_action_script.js * Create ui_page.html * Create ui_page_client_script.js * Create ui_page_processing_script.js * Update README.md * Add files via upload * Updated the table name ui_page_processing_script.js
1 parent 065c442 commit 04c6cbd

File tree

8 files changed

+155
-0
lines changed

8 files changed

+155
-0
lines changed
53.8 KB
Loading
57 KB
Loading
17.2 KB
Loading
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
**Usecase:**
2+
Currenlty there's no OOB feature to share the all the reports from the particular dashboard with the user or group at a time. Also, sharing the dashboard with the user/group doesnot share the corresponding reports with them automatically.
3+
In order to do that, admin or report owner should open each report and share them individually.
4+
If the dashboard has more reports i.e 20+, then it'll take a considerable amount of time to complete this task.
5+
To reduce the manual effort, we can use this custom logic to share all the reports from the particular dashboard at a time.
6+
7+
**Pre-requisite:**
8+
A database view which shows the reports shared with atleast one dashboard need to be created.
9+
ServiceNow community article link which explains how to build one..(Thanks to Adam Stout for this)
10+
https://www.servicenow.com/community/performance-analytics-blog/view-reports-on-a-dashboard-and-dashboards-using-a-report/ba-p/2271548
11+
12+
**Components:**
13+
1. UI Page: It contains Jelly script (HTML), Client script and Processing script. Used to capture the user/group info and share the rports with them.
14+
2. UI Action(Client): Created on the Dashboards (pa_dashboards) table. Used to open the UI page as apopup/modal window
15+
16+
This UI action is visible on the dashboard properties page (image attached)
17+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!--
2+
UI Page details
3+
Name: sj_share_reports
4+
Category: General
5+
-->
6+
7+
<?xml version="1.0" encoding="utf-8" ?>
8+
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
9+
<g:ui_form>
10+
<input type="hidden" id="key" name="key" value="${sysparm_key}"/>
11+
<input type="hidden" id="cancelled" name="cancelled" value="false"/>
12+
13+
14+
<div class="modal-body">
15+
<div class="row form-section form-group">
16+
<div class="control-label">
17+
<div><p><b> Select the group: </b></p></div>
18+
<div>
19+
<g:ui_reference name="group" id="group" query="active=true" table="sys_user_group" field="name"/>
20+
</div>
21+
<div><p><b> Select the user: </b></p></div>
22+
<div>
23+
<g:ui_reference name="user" id="user" query="active=true" table="sys_user" field="name;user_name;email"/>
24+
</div>
25+
</div>
26+
</div>
27+
</div>
28+
<footer class="modal-footer">
29+
<button class="btn-primary btn" style="min-width:5em" onclick="return onSubmit();" name="submit" id="submit">Share</button>
30+
<button class="btn-danger btn" style="min-width:5em" onclick="return onCancel();" name="cancel" id="cancel">Cancel</button>
31+
</footer>
32+
</g:ui_form>
33+
34+
</j:jelly>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function onCancel() {
2+
var c = gel('cancelled');
3+
c.value = "true";
4+
GlideModal.get().destroy();
5+
return false;
6+
}
7+
8+
function onSubmit() {
9+
return true;
10+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
if (cancelled == "false") {
2+
var dashboard_id = key; // sys id of the dashboard from the Jelly script
3+
var group_id = group; // sys id of group from the Jelly script
4+
var user_id = user; //sys id of user from the Jelly script
5+
6+
if (!gs.nil(group_id) || !gs.nil(user_id)) {
7+
groupShare(dashboard_id, group_id);
8+
userShare(dashboard_id, user_id);
9+
response.sendRedirect("pa_dashboards.do?sys_id=" + key);
10+
} else {
11+
response.sendRedirect("pa_dashboards.do?sys_id=" + key);
12+
gs.addErrorMessage("Please select group/user");
13+
}
14+
} else {
15+
response.sendRedirect("pa_dashboards.do?sys_id=" + key);
16+
}
17+
18+
function groupShare(dashboard_id, group_id) {
19+
var db_view = new GlideRecord('u_reports_shared_with_dashboard'); // Database view name
20+
db_view.addEncodedQuery('repstat_report_sys_id!=^dt_dashboard=' + dashboard_id);
21+
db_view.query();
22+
while (db_view.next()) {
23+
var report_id = db_view.rep_sys_id;
24+
25+
var rec = new GlideRecord("sys_report");
26+
rec.get(report_id);
27+
rec.user = "group";
28+
rec.update();
29+
30+
var report_sharing = new GlideRecord('sys_report_users_groups');
31+
report_sharing.addQuery('group_id', group_id);
32+
33+
report_sharing.addQuery('report_id', report_id);
34+
report_sharing.query();
35+
if (!report_sharing.next()) {
36+
var new_record = new GlideRecord('sys_report_users_groups');
37+
new_record.initialize();
38+
new_record.report_id = report_id;
39+
new_record.group_id = group_id;
40+
new_record.insert();
41+
}
42+
43+
}
44+
}
45+
46+
function userShare(dashboard_id, user_id) {
47+
var db_view = new GlideRecord('u_reports_shared_with_dashboard');
48+
db_view.addEncodedQuery('repstat_report_sys_id!=^dt_dashboard=' + dashboard_id);
49+
db_view.query();
50+
while (db_view.next()) {
51+
var report_id = db_view.rep_sys_id;
52+
53+
var rec = new GlideRecord("sys_report");
54+
rec.get(report_id);
55+
rec.user = "group";
56+
rec.update();
57+
58+
var report_sharing = new GlideRecord('sys_report_users_groups');
59+
report_sharing.addQuery('user_id', user_id);
60+
report_sharing.addQuery('report_id', report_id);
61+
report_sharing.query();
62+
if (!report_sharing.next()) {
63+
var new_record = new GlideRecord('sys_report_users_groups');
64+
new_record.initialize();
65+
new_record.report_id = report_id;
66+
new_record.user_id = user_id;
67+
new_record.insert();
68+
}
69+
70+
}
71+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
UI Action details:
3+
4+
Active: True
5+
Name: Share reports
6+
Table: Dashboard [pa_dashboards]
7+
Order: 1000
8+
Action name: share_report
9+
Show update: True
10+
Client: True
11+
List v2 Compatible: True
12+
Form button: True
13+
Form style: Primary
14+
Onclick: shareReport();
15+
16+
*/
17+
18+
function shareReport() {
19+
var modal = new GlideModal("sj_share_reports"); // UI Page id
20+
modal.setTitle("Share Reports");
21+
modal.setPreference('sysparm_key', g_form.getUniqueValue());
22+
modal.render();
23+
}

0 commit comments

Comments
 (0)