Reports in Correct Scope

Utkarsha
Tera Contributor

Hello All, Good day!
I am working on a script to filter out those reports which are not in same scope as that of the dashboard they are placed in..So that I can create them in the correct scope and everything will be placed in one scope only...This task can't be manually as we reports thousands in number...The problem is I am getting all the reports here, not able to filter out the required the reports, I just need those reports which are in different scopes as that of their dashboards and need to create their replicas in the dashboard scope. Could anyone please correct my code here? 
This can be tested in fix script or background script-

Thank you in advance!

I am using below code-

 

var reportList = [];
var grDashboardTab = new GlideRecord('pa_m2m_dashboard_tabs');
grDashboardTab.addEncodedQuery('dashboard.nameSTARTSWITHAnti-Corruption Dashboard');
grDashboardTab.query();
while(grDashboardTab.next()){
    var dashboardScope=grDashboardTab.sys_scope;
    
 var grPaTabs = new GlideRecord('pa_tabs');
 if(grPaTabs.get(grDashboardTab.tab)){
    var grPortal = new GlideRecord('sys_portal');
    grPortal.addQuery('page', grPaTabs.page);
    grPortal.query();
    while(grPortal.next()){
    // gs.print(grPortal.sys_id);
     var  grPortalPreferenced =  new GlideRecord('sys_portal_preferences');
     grPortalPreferenced.addQuery('portal_section', grPortal.sys_id);
     grPortalPreferenced.addQuery('name', 'renderer');
     grPortalPreferenced.addQuery('value', 'com.glide.ui.portal.RenderReport');
     grPortalPreferenced.query();
     while(grPortalPreferenced.next()){
      var grReport = new GlideRecord('sys_report');
      grReport.addQuery('sys_id',grPortalPreferenced.value );
      //grReport.addQuery('sys_scope','NSAMEAS',dashboardScope);
      grReport.query();
      while(grReport.next()){
         
         
         reportList.push( grReport.sys_id.toString());
         
      
      }
     }
     var  grPortalPrefrep =  new GlideRecord('sys_portal_preferences');
     grPortalPrefrep.addQuery('portal_section', grPortal.sys_id);
     grPortalPrefrep.addQuery('name', 'sys_id');
     grPortalPrefrep.query();
     while(grPortalPrefrep.next()){
      var grReportPrefrep = new GlideRecord('sys_report');
      grReportPrefrep.addQuery('sys_id',grPortalPrefrep.value );
   //  grReportPrefrep.addQuery('sys_scope','NSAMEAS',dashboardScope);
      grReportPrefrep.query();
      while(grReportPrefrep.next()){
         reportList.push( grReportPrefrep.sys_id.toString());
      }
     }
    }
}

}
gs.print(reportList);

 

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Utkarsha  

you are not querying with scope

what debugging have you done so far in the script with logs?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hello @Ankur Bawiskar ,
Thank you so much for responding on this.

Actually for testing purpose I had commented out the line-

// grReportPrefrep.addQuery('sys_scope','NSAMEAS',dashboardScope);
grReport.addQuery('sys_scope','NSAMEAS',dashboardScope);
These lines are not working at all....giving all the reports from the system

@Utkarsha  

why not compare exact scope? like this

// grReportPrefrep.addQuery('sys_scope',dashboardScope);

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,
The thing is I can't query like that as I have to filter out the reports which are not in the same scope as that of the dashboard , could you please suggest me query for that?
I tried 'NOTLIKE' but not working
Thanks