How to unpublish a report using script?

HarI98
Tera Expert

Hello community,

How to unpublish a report using script?
I tried switching the is_published to True but if I go to the report then and check if it was published, it wont be.
So it has to be something else that I'm not aware of.

Would appreciate pointers/tips.

Thanks in advance!

1 ACCEPTED SOLUTION

Hi, I ended up raising a HI Case to ask SN about this and this is what they responded. (basically cant do it with script)

find_real_file.png

View solution in original post

15 REPLIES 15

Hi,

the script may not be reliable as we are not sure on how ServiceNow handles it in backend

So rely on manual step

Regards
Ankur

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

Hi, I ended up raising a HI Case to ask SN about this and this is what they responded. (basically cant do it with script)

find_real_file.png

In current UI also you can see publish/unpublish option.

find_real_file.png

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

Hi,

Yes I know about this way, but question was more for doing it within a script.

aen612
Tera Guru

Published reports are available to the role 'public'. You need to remove that role to unpublish a report.

Here's what I did, with an output of URLs that you can go test and verify they are no longer publicly accessible:

function publicReports() {
    var reports = [];
    var grReport = new GlideRecord('sys_report');
    grReport.addEncodedQuery('roles=public');
    grReport.query();
    while (grReport.next()) {
        reports.push(grReport.title + ': https://{INSTANCE_INFO}.service-now.com/sys_report_display.do?sysparm_report_id=' + grReport.sys_id);
        grReport.roles = grReport.roles.replace('public', '');
        grReport.is_published = false;
        grReport.update();
    }
    return reports;
}
var goGetEm = publicReports();
goGetEm;