<h2>How to find references to a report on Homepages and Non responsive Dashboards using JavaScript</h2><br/><div style="overflow-x:auto"><article><div ><h3 >Issue </h3><section><h1 style="color: #d1232b;">Introduction</h1> <hr style="border-top-width: 1px; border-top-style: solid; border-top-color: #cccccc;" /> <div style="margin-left: 15px;">Sometimes it may be useful to find usages of a report on Homepages and non responsive Dashboards. For example, you may want to remove or redesign a report, but want to measure the impact. The script in this article will search for Homepages and Dashboards that have this report.</div> <div style="margin-left: 15px;">If an instance is using a responsive dashboard, the following script might not give accurate results because responsive dashboards use sys_grid_canvas and sys_grid_canvas_pane tables to store contents on the dashboard. </div> <h1 style="color: #d1232b;">Usage</h1> <hr style="border-top-width: 1px; border-top-style: solid; border-top-color: #cccccc;" /> <div style="margin-left: 15px;">The function in the script takes one argument. This should be a string containing the sys ID of the report (sys_report record) you are looking for. This script should be run via the Scripts - Background module.</div> <h1 style="color: #d1232b;">The Script</h1> <pre style="width: 1210px; margin-left: 15px;"><span style="color: #333399;"><strong><span style="font-size: 12pt;">Version for homepages and non-responsive dashboards:</span></strong></span></pre> <hr style="border-top-width: 1px; border-top-style: solid; border-top-color: #cccccc;" /> <pre style="width: 1210px; margin-left: 15px;">findReportReferences(<span class="hljs-string" style="color: #a6e22e;">'c8b0bce8db25b3003869ff041d961975'</span>); //sys_id for the report <span class="hljs-function"><span class="hljs-keyword" style="color: #f92672; font-weight: bold;">function</span> <span class="hljs-title" style="color: #a6e22e; font-weight: bold;">findReportReferences</span>(<span class="hljs-params">report</span>) </span>{ <span class="hljs-keyword" style="color: #f92672; font-weight: bold;">var</span> pp = <span class="hljs-keyword" style="color: #f92672; font-weight: bold;">new</span> GlideRecord(<span class="hljs-string" style="color: #a6e22e;">'sys_portal_preferences'</span>); pp.addQuery(<span class="hljs-string" style="color: #a6e22e;">'value'</span>, report); pp.query(); <span class="hljs-keyword" style="color: #f92672; font-weight: bold;">while</span> (pp.next()) { <span class="hljs-keyword" style="color: #f92672; font-weight: bold;">var</span> page = <span class="hljs-keyword" style="color: #f92672; font-weight: bold;">new</span> GlideRecord(<span class="hljs-string" style="color: #a6e22e;">'sys_portal_page'</span>); page.get(pp.portal_section.page); <span class="hljs-keyword" style="color: #f92672; font-weight: bold;">var</span> tabs = <span class="hljs-keyword" style="color: #f92672; font-weight: bold;">new</span> GlideRecord(<span class="hljs-string" style="color: #a6e22e;">'pa_tabs'</span>); tabs.addQuery(<span class="hljs-string" style="color: #a6e22e;">'page'</span>, page.sys_id); tabs.query(); <span class="hljs-comment" style="color: #75715e;">// No PA tabs, it's just a homepage</span> <span class="hljs-keyword" style="color: #f92672; font-weight: bold;">if</span> (tabs.getRowCount() < <span class="hljs-number">1</span>) { gs.info(<span class="hljs-string" style="color: #a6e22e;">"Homepage: "</span> + page.title); <span class="hljs-keyword" style="color: #f92672; font-weight: bold;">continue</span>; } <span class="hljs-comment" style="color: #75715e;">// PA tabs exist, it's on a dashboard</span> <span class="hljs-keyword" style="color: #f92672; font-weight: bold;">while</span> (tabs.next()) { <span class="hljs-keyword" style="color: #f92672; font-weight: bold;">var</span> m2m = <span class="hljs-keyword" style="color: #f92672; font-weight: bold;">new</span> GlideRecord(<span class="hljs-string" style="color: #a6e22e;">'pa_m2m_dashboard_tabs'</span>); m2m.addQuery(<span class="hljs-string" style="color: #a6e22e;">'tab'</span>, tabs.sys_id); m2m.query(); <span class="hljs-keyword" style="color: #f92672; font-weight: bold;">while</span> (m2m.next()) { gs.info(<span class="hljs-string" style="color: #a6e22e;">"Dashboard: "</span> + m2m.dashboard.name); } } } }</pre> <hr /> <pre style="width: 1210px; margin-left: 15px;"><span style="color: #333399;"><strong><span style="font-size: 12pt;">Version for responsive dashboards:<br /><br /></span></strong></span>var reports = ['01266b8bdb0cf7c46538f7adae9619d0']; // list of report sysIDs<br />var dashboards = [];<br />reports.forEach(function(report) {<br /> dashboards = dashboards.concat(getDashboardsForAReport(report))<br /> dashboards = dashboards.filter(function (elem, index, me) { // remove any duplicates exists<br /> return index === me.indexOf(elem);<br /> });<br />})<br />gs.log(JSON.stringify(dashboards.join()));<br />// +++++++++++++++ helper functions +++++++++++++++<br />function getDashboardsForAReport(sysId) {<br /> var paSysReport = new GlideRecordSecure('sys_report');<br /> var paGaugeReport = new GlideRecordSecure('sys_gauge');<br /> var tabs = [];<br /> if (sysId && paSysReport.get(sysId))<br /> tabs = _getTabsFromWidget(sysId);<br /> if (paGaugeReport.get('report', sysId))<br /> tabs = tabs.concat(_getTabsFromWidget(paGaugeReport.getValue('sys_id')));<br /> tabs = tabs.filter(function (elem, index, me) { // remove any duplicates exists<br /> return index === me.indexOf(elem);<br /> });<br /> var dashboards = [];<br /> tabs.forEach(function (tab) {<br /> dashboards = dashboards.concat(_getDashboardFromTab(tab.tabUniqueId))<br /> });<br /> dashboards = dashboards.filter(function (elem, index, me) { // remove any duplicates exists<br /> return index === me.indexOf(elem);<br /> });<br /> return dashboards;<br />}<br />function _getTabsFromWidget(sysId) {<br /> // fetch tabs<br /> var paTabs = null;<br /> var tabs = [];<br /> var gridCanvasPaneList = [];<br /> var record = null;<br /> var i = 0;<br /> var sysGridCanvasPane = null;<br /> var pref = new GlideRecord('sys_portal_preferences');<br /> pref.addQuery('value', sysId);<br /> pref.query();<br /> var portalSections = [];<br /> while (pref.next())<br /> portalSections.push(pref.getValue("portal_section"));<br /> for (i = 0; i < portalSections.length; i++) {<br /> sysGridCanvasPane = new GlideRecord('sys_grid_canvas_pane');<br /> sysGridCanvasPane.addQuery('portal_widget', portalSections[i]);<br /> sysGridCanvasPane.query();<br /> while (sysGridCanvasPane.next())<br /> gridCanvasPaneList.push(sysGridCanvasPane.getValue('grid_canvas'));<br /> }<br /> for (i = 0; i < gridCanvasPaneList.length; i++) {<br /> paTabs = new GlideRecordSecure('pa_tabs');<br /> paTabs.addQuery('canvas_page', gridCanvasPaneList[i]);<br /> paTabs.query();<br /> while (paTabs.next()) {<br /> record = {<br /> id: paTabs.getValue('sys_id'),<br /> tabUniqueId: paTabs.getValue('sys_id'),<br /> name: paTabs.getDisplayValue('name'),<br /> };<br /> tabs.push(record);<br /> }<br /> }<br /> return tabs;<br />}<br />function _getDashboardFromTab(tabID) {<br /> var dashboards = [];<br /> var dashBoardTab = new GlideRecordSecure('pa_m2m_dashboard_tabs');<br /> var padDashboard = new GlideRecordSecure('pa_dashboards');<br /> var record = null;<br /> dashBoardTab.addQuery('tab', '=', tabID);<br /> dashBoardTab.query();<br /> while (dashBoardTab.next()) {<br /> dashboards.push(dashBoardTab.getDisplayValue('dashboard.sys_id'));<br /> }<br /> return dashboards;<br />}</pre></section></div></article></div>