Client-script: Iterate through the variable_pool?

MattGlew
Giga Contributor

Is it possible, client-side, to loop through all of the form variables (variable_pool) on a requested item form, with a view to making them read-only?

My approach in the past has always been to create an sc_req_item OnLoad client-script (one for each CatItem) and type out each variable individually (well, export vars to Excel, re-format into code & copy back), which is fine, but a bit long in the tooth & can come back to bite you in the future if a new variable is added to the CatItem and you forget to add it to the client script.

It would be a much neater solution if I could simply loop through the variables! I've only been able to find server-side code on the wiki, which isn't a lot of help in this case. I suppose an Ajax call could retrieve the variable_pool array (assuming it's an array) and I could then enumerate that, but there could be hundreds of variables on the page, so that approach would be extremely inefficient.

I've also had a look at the rendered HTML of the page and there's nothing obvious that identifies variable-related widgets / elements, so another dead-end.

Any ideas?

5 REPLIES 5

Lorenzo Persich
ServiceNow Employee
ServiceNow Employee

Another easy way to loop on variables in client script without DOM manipulation is the following:

g_form.elements.forEach(function(element) {
    if (element.tableName == "variable") {
        g_form.nameMap.forEach(function(entry){
            if (entry.realName == element.fieldName) {
                console.log(entry.prettyName); // here is your variable name
            }
        });
    }
});

 

And if you want to set all the variables as read only, you can do so with just one code line

g_form.setVariablesReadOnly(true);