Christopher_Mal
ServiceNow Employee

I am working on a plan to help those that have apps in the global namespace convert those apps into a scoped app.   I'd be willing to hear any thoughts that the community wants to share (either post on this thread and / or email me chris.maloy@servicenow.com).   There are many difficult steps in this process (like migrating to a similar, but different data model as well as identifying which API calls you may be using that need to change because they are not accessible to other scopes).

For this reason I am going to start writing a bunch of scripts that can help in the conversion process.

This is a rough first draft for a helper script that can be called to help identify some global dependencies that will change during your conversion process.   Don't be too critical I wrote this while on a phone call and I don't multi-task well.   I am sure there are better ways to use regular expressions (instead of splitting strings like I am doing - welcome to any feedback).

You will need to call scanScript passing in the sysId of the script you want to scan (including the type if it is not a script include). If you are in a domain separated environment you will need to query with NoDomain.  

var siNameCalls = scanScript('ca4033c1d7110100fceaa6859e610326');

//gs.print("" + identifySIDependencies());

//default script type will be a script include (no need to pass in type unless it is not a script include)
function scanScript(scriptSysId, type) {
                var lgr = new GlideRecord('sys_script_include');

                switch (type) {
                                case 'businessrule':
                                                var lgr = new GlideRecord('sys_script');
                                                break;
                                default:
                                                break;
                }

                lgr.get(scriptSysId);
                if (lgr) {
                                var txt = lgr.script,
                                                ans = txt.split("new"),
                                                anslength = ans.length;


                                while (anslength--) {
                                                var parsedData = ans[anslength];
                                                var dependency = parsedData.substr(0, parsedData.indexOf("("));
                                                //gs.print(dependency);
                                                var result = identifySIDependencies(dependency); //ItomJSONParser is public //JSONParser set to not public unit test values

                                                if (result === 0) {
                                                                gs.print(dependency + " is invalid script include usage. Not accessible in app scope.");
                                                }
                                }
                }
}

function identifySIDependencies(dependency) {
                var sigr = new GlideRecord('sys_script_include'),
                                enc = 'active=true^name=' + dependency.trim();

                //build the lookup query
                sigr.addEncodedQuery(enc);
                sigr.query();

                if (sigr.next()) {
                                //check if public access
                                if (sigr.access == 'public') {
                                                return 1; //public
                                }

                                return 0; //private
                }

                return -1; //not found
}