BLOG: Filter Update for Affected CIs Related list Add button on Outage Form

bhanu_vamshi
Tera Contributor

How to Add a Filter Condition to the “Add” List Popup in the Affected CIs Related List on the Outage Form (ServiceNow)

In ServiceNow, when working with the Outage form, the Affected CIs related list allows users to associate Configuration Items (CIs) with an outage.

When the Add button is clicked, ServiceNow opens a list popup inside a GlideModal that allows users to select CIs.

bhanu_vamshi_0-1773385474915.png

bhanu_vamshi_1-1773385474916.png

 

However, sometimes you may want to restrict which CIs appear in this popup by applying a filter condition (for example, only show CIs with Install Status = Installed).

This article explains how to intercept the popup URL and add a filter condition dynamically.


1. Locate the “Add” Button UI Action

On the Outage form, go to the Affected CIs related list.

  1. Right-click the Add button.

  2. Select Inspect (browser developer tools).

  3. Locate the gsft_id attribute of the button or use the SN_Utils to locate.

 

bhanu_vamshi_2-1773385497605.png

 

Then open the UI Action in ServiceNow.bhanu_vamshi_3-1773385497097.png

 

 

You will see a client-side script similar to the following in the Script field:

 
function openCmdbCIList() {
    var gajax = new GlideAjax("AssociateCIToOutage");
    gajax.addParam("sysparm_name", "getURL");
    gajax.addParam("sysparm_id", g_form.getUniqueValue());
    gajax.addParam("sysparm_add_to", "cmdb_outage_ci_mtom");

    gajax.getXMLAnswer(openList);
}

2. Understand How the List Popup Works

The returned URL is passed to the function openList(), which renders the modal popup.

function openList(url) {
var cmdbciModal = new GlideModal('outage_add_affected_cis');
cmdbciModal.setTitle(getMessage("Add Affected CIs"));
cmdbciModal.setWidth(1200);
cmdbciModal.setAutoFullHeight(true);

cmdbciModal.on('beforeclose', function() {
refreshAffectedCIs();
});

ScriptLoader.getScripts('/scripts/incident/glide_modal_accessibility.js', function() {
cmdbciModal.template = glideModalTemplate;
cmdbciModal.renderIframe(url, function(event) {
glideModalKeyDownHandler(event, cmdbciModal.getID());
});
});

var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'styles/incident_glide_modal.css';
document.head.appendChild(link);
}

This renders the list view inside an iframe, using the URL returned from the server. 

Below is the sample URL which is getting returned when Configuration Class is Configuration Item

outage_add_affected_cis.do?sysparm_table=cmdb_ci&sysparm_add_to=cmdb_outage_ci_mtom&sysparm_stack=no&sysparm_parent_class=cmdb_ci&sysparm_view=associate_ci&sysparm_crSysId=178f7136f1bb22108bb2efdbde054c91&sysparm_crTable=cmdb_ci_service&sysparm_filter

3. Modify the URL to Add a Filter Condition

To apply a filter condition, we intercept the URL and add a sysparm_query parameter.

Example: Filter only Installed CIs

Updated script:

function openCmdbCIList() {
    var gajax = new GlideAjax("AssociateCIToOutage");
    gajax.addParam("sysparm_name", "getURL");
    gajax.addParam("sysparm_id", g_form.getUniqueValue());
    gajax.addParam("sysparm_add_to", "cmdb_outage_ci_mtom");
    gajax.getXMLAnswer(openList);
}

function openList(url) {

    var urlObj = new URL(url, window.location.origin);

    // Get table parameter
    var table = urlObj.searchParams.get("sysparm_table");

    // Apply filter only for cmdb_ci table
    if (table == "cmdb_ci") {
        urlObj.searchParams.set("sysparm_query", "install_status=1");
    }

    var finalUrl = urlObj.toString();

    var cmdbciModal = new GlideModal('outage_add_affected_cis');
    cmdbciModal.setTitle(getMessage("Add Affected CIs"));
    cmdbciModal.setWidth(1200);
    cmdbciModal.setAutoFullHeight(true);

    cmdbciModal.on('beforeclose', function() {
        refreshAffectedCIs();
    });

    ScriptLoader.getScripts('/scripts/incident/glide_modal_accessibility.js', function() {
        cmdbciModal.template = glideModalTemplate;
        cmdbciModal.renderIframe(finalUrl, function(event) {
            glideModalKeyDownHandler(event, cmdbciModal.getID());
        });
    });

    var link = document.createElement('link');
    link.rel = 'stylesheet';
    link.href = 'styles/incident_glide_modal.css';
    document.head.appendChild(link);
}

4. Refresh the Related List After Selection

After selecting CIs and closing the modal, the Affected CIs related list must refresh.

 

 
function refreshAffectedCIs() {
    var listId = g_form.getTableName() + ".cmdb_outage_ci_mtom.outage";

    var list = typeof GlideList2 !== "undefined" ? GlideList2.getByName(listId) : null;
    if (list == null)
        list = typeof GlideList !== "undefined" ? GlideList.get(listId) : null;

    if (list != null)
        list.refresh();
}

5. Important Note

Although the Add button is the same, the table rendered in the popup can change depending on the CI class configuration.

For example, the list may load:

  • cmdb_ci

  • cmdb_ci_service

  • cmdb_ci_telco_circuit

  • or other extended CI tables.

Therefore:

  • Always check the sysparm_table parameter

  • Ensure the query is applied only to the correct table

  • Though, the Query looks similar (for example: Installed Status = Installed) but the backend value will differ based on the sysparm_table i.e., Configuration Class

Example:

if (table == "cmdb_ci") {
    urlObj.searchParams.set("sysparm_query", "install_status=1");
}

 

0 REPLIES 0