Change Report Sharing for multiple reports with a script

Aranya
Tera Guru

Have you ever run into a situation where you had to change the sharing option for multiple reports – from role-based sharing to group based sharing or vice-versa? If yes – you are not alone. The first method that comes to mind is to edit the report and change the Visible To option from the Sharing settings.

Aranya_2-1753511173622.png

 

 

However, this method seems tedious when the situation demands that this change be done for multiple reports. 5 or 10 would have been fine but what if the number of reports to affect is in 100s? This is where today’s scenario comes in.

 

Let us understand which tables are involved when a report is shared to a role, group or a user.

sys_report – This table stores the report configurations which includes the table on which the report is built, the filter configurations, the report styles and the report type. It also stores the roles with which a report is shared in the “Roles” field.

sys_report_users_groups – This table stores the group and the user with which a report is shared.

 

From this we understand which tables stores the sharing settings for a report. So, you might think that adding a role to the “Roles” field of the sys_report table will ensure that the report is shared with the role. However, as it seems, this will not suffice. There is a field called “User” on the sys_report table which toggles/controls this visibility. This field determines whether the report is to be shared with a role, a group or a user. It carries 3 values – each of which point to the type of sharing of the report.

 

GLOBAL – This indicates that the report is shared with everyone. Roles to whom the report is to be shared can be specified in the “Roles” field in the sys_report record.

Group – This indicates that the report is shared with groups or users.

Sys Id of User – This indicates that the report is shared with the user which is done when the type of sharing is “Me”.

 

Let us now take a situation to understand this better. We have almost 70 reports on the change_request table. They are all currently shared with a certain role (this can be inferred from the User field being marked as GLOBAL). Our task is to share all these reports with a group called “Change Managers”.

Aranya_3-1753511173637.png

 

We will now run a fix script which will share all these reports with our required group. It will look something like this.

//// THIS FUNCTION WILL CHECK IF THE REPORT IS ALREADY SHARED TO THE GROUP ////
function checkGroupSharing(reportSysId,groupSysId){
	var checksharing = new GlideRecord('sys_report_users_groups');
	checksharing.addQuery('report_id',reportSysId);
	checksharing.addQuery('group_id',groupSysId);
	checksharing.query();
	return checksharing.hasNext();
}
//// THIS FUNCTION WILL SHARE THE REPORT WITH THE GROUP ////
function shareReportwithGroup(reportSysId,groupSysId){
	var sharegroup = new GlideRecord('sys_report_users_groups');
	sharegroup.initialize();
	sharegroup.setValue('report_id',reportSysId);
	sharegroup.setValue('group_id',groupSysId);
	sharegroup.insert();
}

var checkreport = new GlideRecord('sys_report');
var groupsysid = "a11f12d8c34fa210e8cab342b401310f";  /// SYS ID OF THE GROUP - CHANGE MANAGERS ///
checkreport.addQuery('table','change_request');
checkreport.query();
while(checkreport.next()){
	if(!checkGroupSharing(checkreport.getUniqueValue(),groupsysid)){
		shareReportwithGroup(checkreport.getUniqueValue(),groupsysid);
	}
	checkreport.setValue('user',"group");
}

 

Running this fix script will change the sharing of the report from role-based to group-based.

Note: It is never a good idea to hard-code sys ids. I have done it here for demonstration purposes. 

 

Aranya_4-1753513038176.png Aranya_5-1753513305768.png

 

 

As can be seen, all the reports have been shared with the appropriate group.

5 REPLIES 5

Mark Manders
Mega Patron

Be aware: this only works for the classic reports/dashboards. Platform Analytics (PAE) dashboards and data visualizations are not doing anything with this script.

The classic reports/dashboards are going to be read only as of Australia (only PAE content can be created as of then).


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Aranya 

It looks like it's a situation, not a problem. So, may I suggest adding the word "Blog" in the subject line? This will help provide clarity to the reader—whether it’s a blog post or a question.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

LachezarK
Tera Contributor

Hi, Aranya!
Very useful article you have here 🙂 Thank you for sharing it!
One question - I have a similar request by a customer, but we are aiming for the "Share with Everyone" option. I tried several things, but still don't see such a value and a place where I can set it. Do you have any experience with that?

 

You can share with 'public' and it will be available for everyone, as long as they have access to the data. If ACLs don't allow for the user to see the data, you can share the report, but it won't show anything.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark