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

lss123
Tera Contributor

Hi Rick!  A little late here but I had this same issue today and solved it by creating a Script Include in the proper scope, then calling that from the global scope.  I see that you did that already but it didn't work.  Just wanted to chime in letting you know it worked for me.  If you're still having this issue eight months later, I'd ask what error you were getting with that approach.

Joe72
Tera Contributor

gs.setCurrentApplicationId(sys_app.sys_id);

SaschaWildgrube
ServiceNow Employee
ServiceNow Employee

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

The DevTools app contains a truckload of reusable scripts and
features to support your app development.
Check this out:
https://www.wildgrube.com/servicenow-devtools

CMDB Whisperer
Mega Sage
Mega Sage

@Joe72 nailed it.  First get the current scope using gs.getCurrentApplicationId().  Then find the scope that you want to run the script in and set the current scope using gs.setCurrentApplicationId(scope_id).  Then once you are done, set it back to the original scope.


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

Has anyone tried it though? I tried it from the background script and could not change to another scope. Thanks