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

Mohit Kaushik
Mega Sage
Mega Sage

Hi Harl,

You can write a script Background, scheduled job (if its frequent with certain conditions), fix script etc to achieve this.

Before that you need to see if you have access to that report or not. I guess admin should be able to update all the reports.

Script can be something like this.

var rep = new GlideRecord('sys_report');

rep.addQuery('sys_id','sys id of the report you want to unpublish');

rep.query();

if(rep.next())

{

rep.is_published = false;//This should unpublish the report.

}

 

Please mark this answer as correct and helpful if it resolves the query and helpful alone if it lead you in right direction.

Thanks,

Mohit Kaushik

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

Hi Mohit,

The query I ran to test this in the background script was

var rep = new GlideRecord('sys_report');

rep.get('822c14c1c38102002ba7e219cdba8ffe');
rep.is_published = false;
rep.update();

Even though it updates it on the report form and I can see in the XML it is set on false, when I go to view the report and open the sharing tab it still says "Unpublish" like it's still published.

find_real_file.png

 

Hey Harl,

Please add the below line as well to the script

rep.setWorkflow(false);

before update.

As I ran the query and saw there was another BR(Report roles updated - is_publish field) which was not letting the the version to get updated and making a copy of it.

So disabling other rules will let your script work.

 

Thanks,

Mohit Kaushik

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Switch to Classic UI and you will see the option of Unpublish

find_real_file.png

Regards
Ankur

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