How to create a custom picker view for Location reference field on CMDB CI table

PallaviT
Tera Contributor

Can someone provide me the script logic to build custom category picker(As on Knowledge category reference field as shown in the attached image) on Location reference field on CMDB CI table. It should filter the locations based on Location type as L1 hierarchy. Currently the OOB one represents a custom tree picker view and we wanted to customise it so that the user can select based on Location type as L1 hierarchy.
I have gone through the existing UI Macros, UI page scripts for Knowledge category but not able to understand on how to modify it for Location type requirement. Any help would be highly appreciated.
KB Category picker.png

Thanks in advance!!

1 REPLY 1

Jitendra Diwak1
Kilo Sage

Hi @PallaviT,

 

Please try below code

 

To create a custom category picker for the Location reference field on the CMDB CI table, filtering locations based on Location type as L1 hierarchy, you'll need to customize the client-side script and possibly server-side scripts as well. Here's a basic outline of the logic:

 

1. **Client-side Script**:

   - Modify the existing client-side script for the Location reference field.

   - Use GlideAjax to call a server-side script that retrieves locations based on the Location type as L1 hierarchy.

   - Populate the custom category picker with the filtered locations.

 

2. **Server-side Script**:

   - Create a Script Include or Business Rule to handle the server-side logic.

   - Query the Location table based on the Location type hierarchy (L1).

   - Return the filtered list of locations to the client-side script.

 

Here's a simplified version of what your client-side script might look like:

 

```javascript

function getFilteredLocations() {

    var locationType = g_form.getValue('location_type_field'); // Get the selected Location type

    var ga = new GlideAjax('LocationFilterUtil'); // GlideAjax to call server-side script

    ga.addParam('sysparm_name', 'getFilteredLocations');

    ga.addParam('location_type', locationType);

    ga.getXMLAnswer(function(response) {

        // Populate the custom category picker with filtered locations

        // This will depend on your specific implementation

    });

}

```

 

And here's an example of what your server-side script (Script Include or Business Rule) might look like:

 

```javascript

var LocationFilterUtil = Class.create();

LocationFilterUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getFilteredLocations: function() {

        var locationType = this.getParameter('location_type');

        var locationList = []; // Array to hold filtered locations

        // Query the Location table based on the Location type hierarchy (L1)

        var locationGr = new GlideRecord('cmn_location');

        locationGr.addQuery('location_type', locationType);

        locationGr.query();

        while (locationGr.next()) {

            // Add location details to the locationList array

            locationList.push({

                'sys_id': locationGr.getValue('sys_id'),

                'name': locationGr.getValue('name')

                // Add more fields as needed

            });

        }

        // Return the filtered list of locations as JSON

        return JSON.stringify(locationList);

    }

});

```

 

Ensure you replace `'location_type_field'` with the actual field name for the Location type field on your form. Also, replace `'LocationFilterUtil'` with the appropriate name of your Script Include or Business Rule.

 

This is a basic example to get you started. You may need to adjust the script logic based on your specific requirements and your ServiceNow instance's configuration. Testing thoroughly in a development or test env ironment is recommended before deploying to production.

 

Please accept my solution if it resolves your issue and thumps 👍 up

 

Thanks

Jitendra 

Please accept my solution if it works for and thumps up.