Auto populate list collector values based on another field

sid46
Kilo Contributor

Hello all,

I'm really hoping I can get some help/solution on this as its not working and I am a bit lost on how to make it work given I'm a servicenow newbie.

I have a catalog item with fields for Hostname and IP, and a list collector which shows currently available Tags. Currently the user is able to select any number of tags that are being shown in the list collector.  Now, I have a table setup, lets call it mapping table, which has records of IP address ranges associated to certain tags. What i want to do is once the user puts in the IP address, the list collector should filter and only show tags it finds associated to that IP range in the mapping table. For example, if user puts 10.0.0.1, the list collector should update and only list tags which are associated to that range in table. similarly if user puts 192.168. address it should show tags related to that range.

I did some research and found onChange with include script would be the best way to achieve this, I have the following script but when i open the catalog item it does not update anything. I only have the regex for 10.x.x.x address in script for now, plan on adding 192.168 and 172. regex once this is functional.

 

client 
 
        function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === ''){
                return;
        }
        var collectorName = 'Tags';
        var tagId = g_form.getValue('Tags');
                
                var ga = new GlideAjax('tagCompare');
        ga.addparam('sysparm_name','getTagInfo');
        ga.addParam('sysparm_tag_id', tagId);
        get.getXML(ajaxResponse);
 
        function ajaxResponse(serverResponse){
                var result = serverResponse.responseXML.getElementsByTagName("result");
                var tags_array = result.evalJSON();
                for(var i = 0; i < tags_array.length; i++){
                window[collectorName + 'g_filter'].reset();
        window[collectorName + 'g_filter'].setQuery(tags_array[i]);
        window[collectorName + 'acRequest'](null);
                        
}                
        }
 
}
 
Script include
var tagCompare = Class.create();
tagCompare.prototype = {
    initialize: function() {
    },
getTagInfo: function() {
        var regex = /(^10\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])\.([0-9]|[0-9][0-9]|[0-2][0-5][0-5])$)/gm;
        var serverId = this.getParameter('sysparm_tag_id');
        var gr = new GlideRecord('mapping_table');
        var result = [];
        result = this.newItem("result");
        gr.addQuery('cidr_ip');
        gr.query();
        while(gr.next){
                if (gr.cidr_ip == regex){
                        result = gr.pa_tag;
                }
        }
        return JSON.stringify(result);
        
},
    type: 'tagCompare'
};
 
 
Any help here is highly appreciated!
1 ACCEPTED SOLUTION

okay

In my case I have a table where I have stored IP Address and Corresponding tags for those IP Address as shown in above image. you can ping the screenshot of your table so that I can understand it in a better way.

 

If the string is in begining then use the below code:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === ''){
return;
}
var collectorName = 'tags_name';

alert(g_form.getValue('ip_address'));
window[collectorName + 'g_filter'].reset();
if(g_form.getValue("ip_address").startsWith("10."))
window[collectorName + 'g_filter'].setQuery("u_ip_addressSTARTSWITH10.");
else if(g_form.getValue("ip_address").startsWith("192."))
window[collectorName + 'g_filter'].setQuery("u_ip_addressSTARTSWITH192.");
else if(g_form.getValue("ip_address").startsWith("172.16"))
window[collectorName + 'g_filter'].setQuery("u_ip_addressSTARTSWITH172.16");
window[collectorName + 'acRequest'](null);
}

 

Thanks

View solution in original post

9 REPLIES 9

sid46
Kilo Contributor

Also, based on your script it would work if the list collector is based off the mapping table containing the tag and associated IP address correct? in my scenario, the Tags are stored in a separate table which is being referenced by the list collector.

okay

In my case I have a table where I have stored IP Address and Corresponding tags for those IP Address as shown in above image. you can ping the screenshot of your table so that I can understand it in a better way.

 

If the string is in begining then use the below code:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === ''){
return;
}
var collectorName = 'tags_name';

alert(g_form.getValue('ip_address'));
window[collectorName + 'g_filter'].reset();
if(g_form.getValue("ip_address").startsWith("10."))
window[collectorName + 'g_filter'].setQuery("u_ip_addressSTARTSWITH10.");
else if(g_form.getValue("ip_address").startsWith("192."))
window[collectorName + 'g_filter'].setQuery("u_ip_addressSTARTSWITH192.");
else if(g_form.getValue("ip_address").startsWith("172.16"))
window[collectorName + 'g_filter'].setQuery("u_ip_addressSTARTSWITH172.16");
window[collectorName + 'acRequest'](null);
}

 

Thanks

I have added attachments to show how my table looks. it is taking in the tag and IP information based on reference fields off another table. I tried your updated script but it is not yet fully functional. I really appreciate your help on this!

Okay.. Please call on 9833622526 once.

sid46
Kilo Contributor

**Resolved**

 

For those who may encounter a similar scenario.

 

I was able to get it to work by changing the code a bit from Mahi9315's response. I called the list collector variable directly instead of going through variable. My list collector is named 'Tags'

var ipValue = g_form.getValue('IP_Address');

filterString1 = "<field_name> STARTSWITH10"

 

Tagsg_filter.reset();

 

if(ipValue.startsWith("10.")){

  Tagsg_filter.setQuery(filterString1);

} else{

  ..add code..

}

TagsacRequest(null);

 

 

this successfully got the list collector to filter results upon change on IP address field.