Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

stacylen196
Tera Contributor

That fixed my issue with compilation.  Thanks!!  

Now I have a problem that it is not stopping at breakpoints when running debugger.  I will open another question for that problem