How to filter the Configuration Items field entries from the selected service and service offering in Incident form?

Akshatha Ballal
Tera Expert

I have a requirement to filter the Configuration items reference filed on the incident from to show only those CIs related to the service and service offering selected. I may have to query the relationship table and get only those related records of CIs for the selected service/offering.

How do I achieve this? Any help on script or an idea of way forward (using reference qualifier or anything easier?) is much appreciated.

1 ACCEPTED SOLUTION

Sorry, you are correct. You need to call an encoded query to get the qString to be applied:

var incUtils = Class.create();
incUtils.prototype = {
    initialize: function() {
    },

    /**
     * Filters the Configuration item from Service and Service offering
     * @param  {GlideRecord} incGr The incident record used
     * @return {string}       The reference qualifier string used.
     */
    filterByService: function(incGr) {
        var retArray = [];
        var relGr = new GlideRecord('cmdb_rel_ci');
        var qString = 'parent=' + incGr.business_service + '^ORparent=' + incGr.service_offering;
        relGr.addEncodedQuery(qString);
        relGr.query();
        while (relGr.next()) {
            retArray.push(relGr.child.toString());
        }
        return 'sys_idIN' + retArray;
    },

    type: 'incUtils'
};

View solution in original post

7 REPLIES 7

ccajohnson
Kilo Sage

You would need to create a script that does the lookups then sends back the filter (reference qualifier). Before you begin you should look at the relationship table and try to replicate the filter. It is unclear if you are filtering on both the Service and Service offering, or just one. Once you get the results you desire from looking up manually, then you can build your filter. I would suggest using a script include to store the function used to lookup. If you don't have one, you can always create one that you can re-use for other filtering as well. I would suggest incUtils as a name.

Let us know if you need further assistance in creating the script include and calling the filter function.

I want to filter on both Service and Service Offering. A bit of a help on scripting part and calling the filter function is much appreciated.

ccajohnson
Kilo Sage

Since the CI Relationship [cmdb_rel_ci] table has two reference fields we will use the Parent field to look up the selected Services and use the Child field with the reference qualifier. Here is an example of the script include:

Name: incUtils
API Name: global.incUtils
Client callable: false
Application: Global
Accessible from: All Application Scopes
Script:

var incUtils = Class.create();
incUtils.prototype = {
    initialize: function() {
    },

    /**
     * Filters the Configuration item from Service and Service offering
     * @param  {GlideRecord} incGr The incident record used
     * @return {string}       The reference qualifier string used.
     */
    filterByService: function(incGr) {
        var retArray = [];
        var relGr = new GlideRecord('cmdb_rel_ci');
        var qString = 'parent=' + incGr.business_service + '^ORparent=' + incGr.service_offering;
        relGr.query();
        while (relGr.next()) {
            retArray.push(relGr.child.toString());
        }
        return 'sys_idIN' + retArray;
    },

    type: 'incUtils'
};

To set the reference qualifier, you need to create or change the dictionary override for the Configuration item field:
javascript: new incUtils().filterByService(current);

Doesn't the variable qString needs to be called/added somewhere?