logo

NJP

Link to script include in script field

Import · Jul 05, 2019 · article

Hi,

I created a script that allows you to open the scripts include directly from the code.

To open the link, use ctrl + click.

This example is on Business rule table.

This is the final result:

image

This is the necessary script:

Client script on load, on Business rule table

function onLoad() {

    try {

        setTimeout(function(){

            // Get all elements with class "cm-variable"
            var scriptInclude = g_form.getFormElement().getElementsByClassName('cm-variable');

            for(var i = 0; i < scriptInclude.length; i++) {

                var fName = scriptInclude[i].innerHTML;

                // Retrieve the Script include
                var si = new GlideRecord("sys_script_include");
                si.addQuery("name",fName);
                si.setLimit(1);
                si.query();

                // Generate the link and update html
                while(si.next()){
                    scriptInclude[i].innerHTML = '<a href="/nav_to.do?uri=sys_script_include.do?sys_id='+si.sys_id.toString()+'">'+fName+'</a>';
                }

            }

        }, 3000);

    } catch(exception) {
        jslog('Error in Client Script <script>: ' + exception);
    }

}

Notes- Client script on load and not onChange, to avoid performance issue.- Timeout of 3s necessary, because without it some times script field is loaded after the client script.

- Remember to press Ctrl before the click

View original source

https://www.servicenow.com/community/developer-articles/link-to-script-include-in-script-field/ta-p/2326658