Getting Cross Scope Error even after granting Cross Scope Privileges

Aniket Bhanse
Tera Guru

I am trying to create a simple script include from Fix script (Scoped Application) in the global application (The script include should be created in the global application) and I am getting the error Cross Scope Error. And I have granted the Cross Scope already of Create, Write and Read and Execute API as well. But still the error is there. 

var SIGr = new GlideRecord('sys_script_include');
SIGr.initialize();

SIGr.name = 'MyNewScriptInclude';
SIGr.api_name = 'global.MyNewScriptInclude'; 
SIGr.script = `
var MyNewScriptInclude = Class.create();
MyNewScriptInclude.prototype = {
    initialize: function() {
    },

    myFunction: function() {
        // Your script logic here
        return 'Hello from MyNewScriptInclude!';
    },

    type: 'MyNewScriptInclude'
};
`;
SIGr.client_callable = true;
SIGr.active = true;
SIGr.description = 'This is a sample script include.';
SIGr.sys_scope = "global";
var sysId = SIGr.insert();

if (sysId) {
    gs.info("Script Include created with sys_id: " + sysId);
} else {
    gs.error("Failed to create Script Include. Check for existing sys_id or name conflicts.");
}

 

Please suggest what should I do.

Thank you

18 REPLIES 18

Agree 💯

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


Aniket Bhanse
Tera Guru

@Ankur Bawiskar @GlideFather 

We have already built a Service Bridge so that whenever a button is clicked on the Source Catalog item record (maintain item record), whatver configuration of that maintain record will be migrated to target instance in one click. This is already happening. For example: All variables, all variables sets, UI policy, UI policy actions, Catalog clients, everything is getting moved smoothly. 
The only issue is when there is a GlideAjax in the Catalog client script. Therefore I am looking for such a solution. 

Can you recommend any easier way to do this? This should be automatic (not a manual intervention). 

 

Thank you

OK and the error message 

    gs.error("Failed to create Script Include. Check for existing sys_id or name conflicts.");

is what you get when there is glideAjax in the script include?
Have you checked for a script include that does not have Ajax?

The error message seems to be about existing sys id, have you checked that there is not already existing script include with the same name or sys id?

Your procedure is to create new things only or for updates as well? If somebody is modifying existing record, your script only condisers .insert() which is to create new records, but if somebody adds new lines to script include (which I guess happens frequently) then it should check whether the record exists or not, if not then create it, if yes then update it...

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


So we have different functions. 

For update, it will automatically update the script include record (which is working smoothly)

 

The client script which do not have or which do have "GlideAjax", both client scripts are getting moved properly. But the script include when there there is a GlideAjax in the client script, that script include is not getting moved (only when there is an insert operation). If the script include already exists in the target instance, then it will smoothly update. 

Yes, I am getting that error in the log table. 

The sys_id or script include_name do not exist in the target table script include.

@Aniket Bhanse 

I was able to create Script Include from background script of scoped app with these settings

1) "Can Create" checkbox True on Script Include table

AnkurBawiskar_0-1750167638207.png

 

2) These cross scope privileges record

AnkurBawiskar_1-1750167666826.png

Script

var scriptIncludeName = 'MyNewScriptInclude';
var apiName = 'global.MyNewScriptInclude';

// Check if Script Include already exists
var existing = new GlideRecord('sys_script_include');
existing.addQuery('name', scriptIncludeName);
existing.query();

if (!existing.hasNext()) {
    var SIGr = new GlideRecord('sys_script_include');
    SIGr.initialize();

    SIGr.name = scriptIncludeName;
    SIGr.api_name = apiName;
    SIGr.script = "var MyNewScriptInclude = Class.create();\nMyNewScriptInclude.prototype = {\n    initialize: function() {\n    },\n\n    myFunction: function() {\n        // Your script logic here\n        return 'Hello from MyNewScriptInclude!';\n    },\n\n    type: 'MyNewScriptInclude'\n};";
    SIGr.client_callable = true;
    SIGr.active = true;
    SIGr.description = 'This is a sample script include.';
    // SIGr.sys_scope = <sys_id_of_scope>; // Omit or set to correct sys_id if needed

    var sysId = SIGr.insert();

    if (sysId) {
        gs.info("Script Include created with sys_id: " + sysId);
    } else {
        gs.error("Failed to create Script Include. Check for required fields or permissions.");
    }
} else {
    gs.error("A Script Include with the name '" + scriptIncludeName + "' already exists.");
}

AnkurBawiskar_4-1750167793450.png

 

Output: Script Include created

AnkurBawiskar_2-1750167690744.png

AnkurBawiskar_3-1750167725818.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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