Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Use Fix Script to Set Reports False - wont compile

stacylen196
Tera Contributor

I have the below Fix Script copied from internet.

It looks good to me.  However, I cannot get it to compile.  See errors posted, below script

Any assistance appreciated

StacyLen

 

************* Script ******************************

// Fix script to set the 'Public' field of all reports to false

// Create a new GlideRecord object for the 'sys_report' table
var gr = new GlideRecord('sys_report');

// Query all records in the 'sys_report' table
gr.query();

// Iterate through each report record
while (gr.next()) {
    // Check if the report is currently public
    if (gr.public == true) {
        // Set the 'public' field to false
        gr.public = false;
        // Update the record
        gr.update();
        gs.info("Report '" + gr.title + "' (sys_id: " + gr.sys_id + ") set to private.");
    }
}

gs.info("Fix script completed: All public reports have been set to private.");

 

 

***********Error Msg********************************************************

Script compilation error: Script Identifier: <refname>, Error Description: missing name after . operator (<refname>; line 12), Script ES Level: 0, Interpreted Mode: true
Javascript compiler exception: missing name after . operator (<refname>; line 12) in:

 

1 ACCEPTED SOLUTION

YaswanthKurre
Tera Guru

Hi @stacylen196 ,

 

The issue you're encountering is because public is a reserved word in JavaScript, the language used for scripting in ServiceNow. When you try to access the field using the dot-walking syntax like gr.public, the JavaScript parser throws an error (which you see as a "compile" error) because it mistakes the field name for the reserved keyword.

 

To  fix this, you should use the setValue() and getValue() methods of the GlideRecord object instead of direct dot-walking for the field name public.

 

// Fix script to set the 'Public' field of all reports to false

var gr = new GlideRecord('sys_report');
gr.query();

while (gr.next()) {
    // Get the value of the 'public' field
    var isPublic = gr.getValue('public');

    if (isPublic == 'true') {
        // Set the 'public' field to false
        gr.setValue('public', 'false');
        gr.update();
        gs.info("Report '" + gr.getValue('title') + "' (sys_id: " + gr.getUniqueValue() + ") set to private.");
    }
}

 

Mark this as helpful and correct, if this helps you.

 

Thanks,

Yaswanth

 

View solution in original post

5 REPLIES 5

YaswanthKurre
Tera Guru

Hi @stacylen196 ,

 

The issue you're encountering is because public is a reserved word in JavaScript, the language used for scripting in ServiceNow. When you try to access the field using the dot-walking syntax like gr.public, the JavaScript parser throws an error (which you see as a "compile" error) because it mistakes the field name for the reserved keyword.

 

To  fix this, you should use the setValue() and getValue() methods of the GlideRecord object instead of direct dot-walking for the field name public.

 

// Fix script to set the 'Public' field of all reports to false

var gr = new GlideRecord('sys_report');
gr.query();

while (gr.next()) {
    // Get the value of the 'public' field
    var isPublic = gr.getValue('public');

    if (isPublic == 'true') {
        // Set the 'public' field to false
        gr.setValue('public', 'false');
        gr.update();
        gs.info("Report '" + gr.getValue('title') + "' (sys_id: " + gr.getUniqueValue() + ") set to private.");
    }
}

 

Mark this as helpful and correct, if this helps you.

 

Thanks,

Yaswanth

 

Ankur Bawiskar
Tera Patron

@stacylen196 

the error is because there is no public field on sys_report table

what's your actual business requirement?

AnkurBawiskar_0-1762497664415.png

 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

You are correct in that public is not actually in the sys_report table, but somehow it is an attribute related to Reports.  I copied this script verbatim from one someone had shared on the SN Forums

 

My goal is two fold:  1. learn more about SN Scripting, 2.  To have a script find 'published' reports and set publish to False

@stacylen196 

I am not sure how the other response marked as correct helped you since public is not a field on sys_report table?

 

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