logo

NJP

"Preview Module" Tool

Import · Oct 15, 2018 · article

Included in the Developer Toolbox Update Set available on Share (link to Share in the post).

The "Preview Module" Related Links UI Action will open a new tab/window to display the records or page based on the current settings of the Link Type tab. It's kind of the reverse functionality of the "Create a Module from this Query" Tool.

So, if you have the following setup:

image

...the following list view will appear in a new tab/window:

image

Supports the following Link types:

  • -- None --
  • Assessment
  • Documentation Link
  • Homepage
  • List Filter
  • List of Records
  • Map Page
  • New Record
  • Run a Report
  • Single Record
  • Timeline Page
  • URL (from Arguments)

It assumes a Link type of "-- None --" to be "List of Records", just like the platform does.

The nice thing about this UI Action is you can modify the settings of the Module and test it out without having to save the record each time, so you can make tweaks and test without waiting for the form to refresh and then select the Module. The UI Action does NOT save the record or make any changes, it just opens the new tab/window.

Here are the details of the UI Action record:

Name: Preview Module

Table: Module [sys_app_module]

Order: 200,000

Action name: u_fpc_preview_module

Active: Checked

Show insert: Checked

Show update: Checked

Client: Checked

Form link: Checked

Hint: Preview the current configuration of this Module record in a new tab/window (FPC)

Onclick: uFpcPreviewModule();

Condition: current.canRead()

Script:

function uFpcPreviewModule() {
    var linkType = g_form.getValue("link_type").toLowerCase();
    if (!linkType)
        linkType = "list"; //default to "List of Records"

    if (["assessment", "detail", "direct", "doc_link", "filter", "homepage", "list", "map", "new", "report", "timeline"].indexOf(linkType) > -1) {
        var tableName = g_form.getValue("name");
        var viewName = g_form.getValue("view_name");
        var condition = g_form.getValue("filter");
        var arguments = decodeURIComponent(g_form.getValue("query"));
        var base = "";
        var param = "";
        var paramValue = "";
        switch (linkType) {
            case "assessment": //Assessment
                base = "assessment_take2.do";
                param = "sysparm_assessable_type";
                paramValue = g_form.getValue("assessment");
                condition = "";
                viewName = "";
                break;

            case "doc_link": //Documentation Link
                base = "https://docs.servicenow.com/?context=" + arguments;
                condition = "";
                viewName = "";
                break;

            case "homepage": //Homepage
                base = "home.do";
                condition = "";
                viewName = "certification_overview";
                break;

            case "list": //List Filter
            case "filter": //List of Records
                base = tableName + "_list.do"; //show list
                break;

            case "map":  //Map
                base = "$map_page_primary.do";
                param = "sysparm_name";
                paramValue = "";
                //try to get the name of the Map page
                var mapPage = gel("sys_display.sys_app_module.map_page");
                if (mapPage.value.trim() != "") {
                    paramValue = mapPage.value.trim();
                }
                condition = "";
                viewName = "";
                break;

            case "report": //Run a Report
                base = "sys_report_template.do";
                param = "jvar_report_id";
                paramValue = g_form.getValue("report");
                condition = "";
                viewName = "";
                break;

            case "detail": //Single Record
            case "new": //New Record
                base = tableName + ".do"; //show form
                break;

            case "timeline": //Timeline Page
                base = "show_schedule.do";
                param = "sysparm_timeline_page_id";
                paramValue = g_form.getValue("timeline_page");
                condition = "";
                viewName = "";
                break;

            case "direct": //URL (from Arguments)
                base = arguments;
                condition = "";
                viewName = "";
                break;
        }

        //build the proper URL
        var url = new GlideURL(base);
        if (viewName) {
            url.addParam("sysparm_view", viewName);
        }
        if (condition != "") {
            url.addParam("sysparm_query", condition);
        }
        if (linkType == "filter") {
            url.addParam("sysparm_filter_only", "true");
        }
        if (linkType == "report") {
            url.addParam("sysparm_from_list", "true");
        }
        if (linkType == "timeline") {
            url.addParam("sysparm_page_schedule_type", "timeline_page");
        }
        if (param != "") {
            url.addParam(param, paramValue);
        }
        window.open(url.getURL(), "_blank");
    } else {
        g_form.addErrorMessage(getMessage("u_fpc_preview_module.not.supported"));
    }
}

Attached are XML files for the UI Action and UI Message records so you can just import them into your instance.

As usual, test it out in your dev instance, or better yet, your personal dev instance first.

I've standardized on using UI Messages now for any message visible to users to make it easier to localize the tools.

Update: June 25, 2022

Supports most of the Link types now.

View original source

https://www.servicenow.com/community/developer-blog/quot-preview-module-quot-tool/ba-p/2268715