logo

NJP

Unlocking Efficiency: Harnessing the Power of Code Reusability with UI Scripts for Seamless Development in ServiceNow

thesnowmonk.com · Sep 23, 2022 · article

Here’s a cool little reusable snippet from my experiments with UI Script. The use case I was dealing with was toggling the visibility of a couple of variables for a catalog item. Now I had to build this particular solution since I was dealing with close to a 100 variables and setting up UI policies for each and every variable was close to impossible. So here’s a reusable little piece of code you can use to achieve the same.

function SampleApplicationUtil() {
  this.vendorMasterList = ['app_1','app_2','app_3','app_4','app_5',
                        'app_6','app_7','app_8','app_9','app_10'];
}

SampleApplicationUtil.prototype.getExclusionList = function(apps) {
    var subsetSet = {};

    // Create a hash map of the subset elements for efficient lookup
    for (var i = 0; i < apps.length; i++) {
        subsetSet[apps[i]] = true;
    }

    var exclusionList = [];

    // Iterate over the superset array and check if each element is in the subset hash map
    for (var j = 0; j < this.vendorMasterList.length; j++) {
        if (!subsetSet[this.vendorMasterList[j]]) {
            exclusionList.push(this.vendorMasterList[j]);
        }
    }

    return exclusionList;
};

SampleApplicationUtil.prototype.setReadOnly = function(g_form, app, val) {
  g_form.setReadOnly(app, val);
};

SampleApplicationUtil.prototype.setVisible = function(g_form, app, val) {
    g_form.setVisible(app, val);
}

SampleApplicationUtil.prototype.setCheckBox = function(g_form, app, val) {
    g_form.setValue(app, val);
}

SampleApplicationUtil.prototype.processExclusionList = function(g_form, apps) {
    for(var i = 0; i < apps.length; i++) {
        this.setCheckBox(g_form, apps[i], false);
        this.setVisible(g_form, apps[i], false);
    }
}

SampleApplicationUtil.prototype.processInclusionList = function(g_form, apps) {
    for(var i = 0; i < apps.length; i++) {
        this.setCheckBox(g_form, apps[i], true);
        this.setVisible(g_form, apps[i], true);
    }
}

Now, the usage is pretty simple. In your UI policy or client script you can invoke the code as follows,

var sau = new SampleApplicationUtil();
var inclusionList = ['app_1', 'app_3', 'app_5'];
var exclusionList = vru.getExclusionList(inclusionList);
sau.processExclusionList(g_form, exclusionList);
sau.processInclusionList(g_form, inclusionList);

The post Unlocking Efficiency: Harnessing the Power of Code Reusability with UI Scripts for Seamless Development in ServiceNow first appeared on thesnowmonk.com.

View original source

https://thesnowmonk.com/index.php/2022/09/23/unlocking-efficiency-harnessing-the-power-of-code-reusability-with-ui-scripts-for-seamless-development-in-servicenow/?utm_source=rss&utm_medium=rss&utm_campaign=unlocking-efficiency-harnessing-the-power-of-code-reusability-with-ui-scripts-for-seamless-development-in-servicenow