Based on the dropdown selection show list collector records

AishwaryaS1
Kilo Sage

We have a custom locations field of type List Collector. 

Also have a dropdown field "What type of location is affected" with choices:

  1. Enterprise Wide
  2. Region
  3. Country
  4. Select Locations
  • When Region=True display a list collector field titled "Regions" which only shows the regions on the cmn_location table (Mandatory)
  • When Country=true display a list collector field titled "Countries" which only shows the countries on the cmn_location table (Mandatory)
  • When Select Locations=True display a list collector field titled "Locations" of all of the locations on the cmn_location table (Please modify the attributes of the list collector so that siteID is displayed in the first column) (Mandatory)
Aishwarya Shelake
2 ACCEPTED SOLUTIONS

@JenniferRah I want this functionality on change_request and incident form not for the catalog item.

Aishwarya Shelake

View solution in original post

AishwaryaS1
Kilo Sage

I've tried the below code and it works:

1. Create the script include:

Name: GetLocations: 

var GetLocations = Class.create();
GetLocations.prototype = {
    initialize: function() {},
    fetch_locations: function(loc_type) {
        var ar = [];
        var listCollectorValues = [];
        var gr = new GlideRecord('cmn_location');
        gr.query();
        while (gr.next()) {
            if (loc_type == 'country' && gr.country != "") {
                //push records in array whose location type is country and country is not empty          
                ar.push(gr.sys_id.toString());
            } else if (loc_type == 'region' && gr.u_region != "") {
                //push records in array whose location type is region and region is not empty          
                ar.push(gr.sys_id.toString());
            } else if (loc_type == 'select locations' && gr.name != "") {
                //push records in array whose location type is select locations            
                ar.push(gr.sys_id.toString());
            }
        }
        for (var i = 0; i < ar.length; i++) {
            var gr1 = new GlideRecord('cmn_location'); // to find the record in location table
            if (gr1.get('sys_id', ar[i])) {
                listCollectorValues.push(gr1.sys_id); // If found-->push the sys_id to the list collector values
            }
        }
        return 'sys_idIN' + listCollectorValues;//returning list
    },
    type: 'GetLocations'
};
 
2. In the list collector field add the advanced reference qualifier:
javascript&colon; new global.GetLocations().fetch_locations(current.u_what_type_of_location_is_affected);
 
u_what_type_of_location_is_affected --> This is the dropdown field which I've created based on this locations will be display
Aishwarya Shelake

View solution in original post

4 REPLIES 4

JenniferRah
Mega Sage

A List Collector is like a Reference field with multiple values. So it stores the sys_ids of the records selected and will show the display value of the record. So if you make 3 list collectors pointing to the Locations table, they will each show the locations themselves, not the specific fields you mentioned.

 

What you seem to be describing is that you want to show a selection list with different fields pulled from the cmn_location table. If it's okay to be able to select just one choice, maybe a Lookup Select Box would be a better choice. Then you can do something like this: 

JenniferRah_0-1729170857873.png

That would only allow you to select one value, though. Is that what you are needing? If not, can you give some more detail?

@JenniferRah I want this functionality on change_request and incident form not for the catalog item.

Aishwarya Shelake

Sorry. I made an assumption since you said "List Collector". Those are only for Catalog Items. The same principle applies to List fields, though. They will only show the default value of the table.

 

You can use a Choice field or String with drop-down and write a client script to dynamically pull the values from the cmn_location table, but that seems a bit cumbersome. If this data doesn't change often, it might be best just to create a Choice field and manually enter the choices. Again, that would be a single-select solution. 

AishwaryaS1
Kilo Sage

I've tried the below code and it works:

1. Create the script include:

Name: GetLocations: 

var GetLocations = Class.create();
GetLocations.prototype = {
    initialize: function() {},
    fetch_locations: function(loc_type) {
        var ar = [];
        var listCollectorValues = [];
        var gr = new GlideRecord('cmn_location');
        gr.query();
        while (gr.next()) {
            if (loc_type == 'country' && gr.country != "") {
                //push records in array whose location type is country and country is not empty          
                ar.push(gr.sys_id.toString());
            } else if (loc_type == 'region' && gr.u_region != "") {
                //push records in array whose location type is region and region is not empty          
                ar.push(gr.sys_id.toString());
            } else if (loc_type == 'select locations' && gr.name != "") {
                //push records in array whose location type is select locations            
                ar.push(gr.sys_id.toString());
            }
        }
        for (var i = 0; i < ar.length; i++) {
            var gr1 = new GlideRecord('cmn_location'); // to find the record in location table
            if (gr1.get('sys_id', ar[i])) {
                listCollectorValues.push(gr1.sys_id); // If found-->push the sys_id to the list collector values
            }
        }
        return 'sys_idIN' + listCollectorValues;//returning list
    },
    type: 'GetLocations'
};
 
2. In the list collector field add the advanced reference qualifier:
javascript&colon; new global.GetLocations().fetch_locations(current.u_what_type_of_location_is_affected);
 
u_what_type_of_location_is_affected --> This is the dropdown field which I've created based on this locations will be display
Aishwarya Shelake