The CreatorCon Call for Content is officially open! Get started here.

Dictionary Override - Reference Qualifier Question

Lucky1
Tera Guru

Hello all,

 

Good day!!

 

On the Incident form, when a configuration item is selected from "Mapped Application Service" (cmdb_ci_service_discovered) class, then only their related Services has to be shown for selection in the "Service" (business_service) field, but not all services. 
For this I have written a Script include, but 

the problem here is, when I open the "Service" field on the dictionary level, I see there is a Dictionary Override, on the Incident. and that is oob one.

javascript:new TaskUtils().getConfigurationItemFilter(current);

 

Now without disturbing the existing functionality, I want to include my script include results also to this reference qualifier, like if(sys_class_name == 'cmdb_ci_service_discovered'), 

 

My Script Include:

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

    getServicesForCI: function(ciSysId) {
       // var ciSysId = this.getParameter('sysparm_ci_sysid');
        gs.log("Results are Configuration item sys_id " + ciSysId);
        var serviceIds = [];

        if (!ciSysId) {
            return serviceIds;
        }
        // Step 1: Find offering(s) where child = selected CI (Mapped Application Service)
        var offeringGR = new GlideRecord('cmdb_rel_ci');
        //offeringGR.addQuery('child', ciSysId);
        //offeringGR.addQuery('parent.sys_class_name', 'service_offering');
        offeringGR.addEncodedQuery('parent.sys_class_name=service_offering^child.sys_id=' + ciSysId);
        offeringGR.query();

        while (offeringGR.next()) {

            var offeringId = offeringGR.parent.toString();
            gs.log("Results are Inside 1st While " + offeringId);


            // Step 2: For each offering, get parent services (business or technical)
            var serviceGR = new GlideRecord('cmdb_rel_ci');
            // serviceGR.addQuery('child', offeringId);
            // serviceGR.addQuery('parent.sys_class_name', 'IN', 'cmdb_ci_service_business,cmdb_ci_service_technical');
            serviceGR.addEncodedQuery('parent.sys_class_name=cmdb_ci_service_business^ORparent.sys_class_name=cmdb_ci_service_technical^child.sys_id=' + offeringId);
            serviceGR.query();

            while (serviceGR.next()) {
                //  gs.log("Results are Inside 2nd While");
                serviceIds.push(serviceGR.parent.toString());
                gs.log("Results atLast are " + serviceIds); //some duplicate sys_ids are coming here
            }
        }

        // return serviceIds;
        return JSON.stringify(serviceIds);
    },
    type: 'Incident_cmdb_Utils'
};
 
 
So, can someone please help me here?
 
 
 
Regards,
Lucky
10 REPLIES 10

Can you tell me is my script correct???

 

Script Include:

 

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

    getServicesForCI: function(ciSysId) {
        if (current.cmdb_ci.sys_class_name == 'cmdb_ci_service_discovered') {

            gs.log("Results are Configuration item sys_id " + ciSysId);
            var serviceIds = [];

            if (!ciSysId) {
                return serviceIds;
            }
            // Step 1: Find offering(s) where child = selected CI (Mapped Application Service)
            var offeringGR = new GlideRecord('cmdb_rel_ci');
            //offeringGR.addQuery('child', ciSysId);
            //offeringGR.addQuery('parent.sys_class_name', 'service_offering');
            offeringGR.addEncodedQuery('parent.sys_class_name=service_offering^child.sys_id=' + ciSysId);
            offeringGR.query();

            while (offeringGR.next()) {

                var offeringId = offeringGR.parent.toString();
                gs.log("Results are Inside 1st While " + offeringId);


                // Step 2: For each offering, get parent services (business or technical)
                var serviceGR = new GlideRecord('cmdb_rel_ci');
                // serviceGR.addQuery('child', offeringId);
                // serviceGR.addQuery('parent.sys_class_name', 'IN', 'cmdb_ci_service_business,cmdb_ci_service_technical');
                serviceGR.addEncodedQuery('parent.sys_class_name=cmdb_ci_service_business^ORparent.sys_class_name=cmdb_ci_service_technical^child.sys_id=' + offeringId);
                serviceGR.query();

                while (serviceGR.next()) {
                    //  gs.log("Results are Inside 2nd While");
                    serviceIds.push(serviceGR.parent.toString());
                    gs.log("Results atLast are " + serviceIds); //some duplicate sys_ids are coming here
                }
            }

            // return serviceIds;
            return JSON.stringify(serviceIds);
        } else {
            return new TaskUtils().getConfigurationItemFilter(current);
        }



    },



    type: 'Incident_cmdb_Utils'
};

https://developer.servicenow.com/dev.do#!/learn/courses/xanadu/app_store_learnv2_scripting_xanadu_sc...  

 

Check if this helps.

 

But if you have already written your logic in new SI , you can put the logic in if condition and in else condition you can call the oob SI and in dictionary override you can then call your new SI. Its should work as expected i believe.


Raghav
MVP 2023
LinkedIn

Hello Raghav,

 

Yes, it's working.

But can you tell me if there will be any consequences, here?

The oob Reference qualifier: 
javascript:new TaskUtils().getConfigurationItemFilter(current);

I have written a script include, where when my condition matches, it gives us the results and if my condition doesn't matches then oob script include will fire.

 

So, I have changed the ref qualifier like below:
javascript:new Incident_cmdb_Utils().getServicesForCI(current.cmdb_ci);


If you see, in the function  parameter, it is current.cmdb_ci.
But before I modify this, the oob ref qualifier is taking , only current.

 

So, is this ok? as I am calling oob script include if my condition not matches?

 

 

 

Regards,

Lucky

 

@Lucky1 It will impact the OOB one, what you need to do is pass the current object and het that as parameter in your SI function.  from the current object you can get cmsb_ci valie in your script include itself. 

 

So something like this will work:

 

Reference qualifier : javascript:new Incident_cmdb_Utils().getServicesForCI(current);

 

Script Include:

 

getServicesForCI : function(currObj) // current object from ref qual

{

if(currObj.cmdb_ci == ' your condition')

{

// Add your code

}

else{

new TaskUtils().getConfigurationItemFilter(currObj); // current object from ref qual

}

}

 


Please mark the answer correct/helpful accordingly.


Raghav
MVP 2023
LinkedIn

@Lucky1 Did this clarify your doubt?


Raghav
MVP 2023
LinkedIn