logo

NJP

Multi Row Variable Set: Limit Rows

Import · Sep 15, 2020 · article

To limit rows a quick search brought us to several sources mentioning:

var field = g_form.getField("multiple_approvers");  // multi variable set name
if (field != null) {
    field.max_rows_size = 3;  // number of rows allowed to be entered
}

This will make the 'Add'-button read only once you have reached the set limit (3 in the example above).

The problem

However, that is only working in the Service Portal/Mobile. As getField is not supported in Desktop.

To set the limit in both the Service Portal and Desktop you can use this script, which is a combination of several.

Solution: onLoad Catalog Client script

UI Type: All

Type: onLoad

Replace variable_set_1 with the name of your Multi Row Variable Set.

Replace the variable_set_id with the sys_id of your Multi Row Variable Set.

function onLoad() {
    try { // try in service portal.
        var field = g_form.getField("variable_set_1");
        if (field != null) {
            field.max_rows_size = 3;
        }
    }
    catch(e){ //desktop
        function setMaxRowSize (sysIdMRWS, limit) {
            var tableVariable = typeof tableVariableCache !== "undefined" &&
            tableVariableCache["table_variable_buttons_" + sysIdMRWS] != null ?
            tableVariableCache["table_variable_buttons_" + sysIdMRWS] :
            typeof table_variable !== "undefined" ? table_variable: null;
            if (tableVariable != null) {
                if (typeof tableVariable.setMaxRowSize === "function") {
                    tableVariable.setMaxRowSize(String(limit));
                } else {
                    tableVariable.maxRowSize = limit;
                }
            }
        }
        setMaxRowSize("variable_set_id", 3);
    }
}

The result:

After the first 2 you still see the Add button is active (left Service Portal, right back-end)

imageimage

After adding the 3th it is not active anymore

imageimage

Other articles on Multi Row Variable sets:

Multi Row Variable Set: Clear/Remove all values

Update value in Multi Row Variable Set (MRVS)

View original source

https://www.servicenow.com/community/developer-articles/multi-row-variable-set-limit-rows/ta-p/2321206