How do you set the application scope in a script

rickseiden_info
Giga Contributor

There has to be a way to do this.

I need to delete about 28,000 records that were created by mistake during an import (I forgot to set the number field to coalesce=true!).   Normally, I would just go to sys.scripts.do, do an encoded query on the table, and then run deleteMultiple.   But it's a scoped application, and it's not letting me run deleteMultiple from the Global scope.

The little dropdown at the bottom that allows you to specify what scope you want to run it in only contains "global".

How do I run a script in a specific application scope?

This is the script I tried to run:

var hr=new GlideRecord('sn_hr_core_case');

hr.addEncodedQuery('sys_created_by=rick seiden');

hr.deleteMultiple();

But it said "Security restricted: Delete operation against 'sn_hr_core_case' from scope 'rhino.global' has been refused due to the table's cross-scope access policy"

I tried to add a sys_scope_privilege for GlideRecord.deleteMultiple, and got the same result.

Thanks for your help

Rick Seiden

PS: If this is a bad idea to try and allow deleteMultiple from the Global Scope, or run a script in a specific scope from sys.scripts.do, please tell me how to mass delete 28,000 records without deleting all records on the table (I want to keep some of them).

PPS:   I tried writing a script include in the proper scope and calling that script include from the Global scope.   That didn't work.

PPPS:   I tried exporting the records I want to keep to an XML file and then using the Delete All Records feature of the Table module on the table.   I got the same error as running it from sys.scripts.do

14 REPLIES 14

Yeah it may have changed along the way.  I tried it before posting the comment above, and although at that time it was running in a different context I did get it to work then.  But I too could not verify the functionality from a background script when I attempted to recently...


The opinions expressed here are the opinions of the author, and are not endorsed by ServiceNow or any other employer, company, or entity.

ok thanks... 

i have just been suggested to have a global script which works on the return of a scoped script include to delete records.

SanketLandge1
Kilo Guru

Hello @rickseiden_info 

 

its only possibility is to run the script in background script for the application created by the instance admin (starting with x_) for anything that is available with sn_ will not show up.

 

Only option being to create cross-scope or create fix script in required application scope & then use it.

 

Thanks,

Sanket Landge

@SanketLandge1 

@rickseiden_info 

@Joe72 

 

As per my earlier post:

You can use DevTools' RunScriptInScope() function to execute code in different scopes. 

Check this out:
https://www.wildgrube.com/servicenow-devtools

The RunScriptInScope() function uses the GlideScopedEvaluator class in a special way.

Please note that different topics have been discussed in this thread:

1. Running a script AS IF the script was contained in an application file in a different scope (this is where the GlideScopedEvaluator class as implemented in the DevTools can help).

2. Changing the scope of the current user session (this is what an admin can select on the top right of the navigation bar). The scope of the user session determines the scope in which changes are made and into which scope new Application File records are placed.

This is what you can set using this function:

gs.setCurrentApplicationId();
However the function can only be executed in global scope, so in order to change the current user's scope in a scoped script, you will need to apply the technique as described in point 1.
3. Making changes to records that are in tables which are in a different scope - IF the table is not configured for cross-scope access: here again, the only way is to execute the script that actually performs the query and the modification in the scope of the table - again using the method in point 1.
I hope that helps!

 

PS: Fix scripts are evil! Do not use fix scripts! Never ever!

That explains the difference, thank you.  When I posted above I was doing the same thing with GlideScopedEvaluator which seems to be what made the difference in my use case at the time.


The opinions expressed here are the opinions of the author, and are not endorsed by ServiceNow or any other employer, company, or entity.