ServiceNow filter ticket configuration item by parent service offering

 where a service offering is selected, filter the configuration items linked to it via cmdb_rel_ci

 

 

1) modify the dictionary override ref qual to call a custom script include


2) script include (not client callable) script: 



var custom_taskUtils = Class.create(); custom_taskUtils.prototype = { DEFAULT_CI_FILTER: gs.getProperty('custom_ci_filter'), TABLES_IN_SCOPE: 'incident,problem,change_request', //--can also leverage sys property 'com.snc.task.principal_class_filter'; initialize: function() {}, getConfigurationItemFilter: function(current) { if (!current) { gs.error("[TaskUtilsSNC.getConfigurationItemFilter] : Invalid parameter"); return; } var configItemFilter = this.DEFAULT_CI_FILTER; try { //gs.log(current.number + ':: SERVICE OFFERING: ' + current.service_offering + ', ' + current.service_offering.name, 'scrpIncl:getConfigurationItemFilter'); if (!gs.nil(current.service_offering)) { var sTaskType = current.getTableName(); if (this.TABLES_IN_SCOPE.indexOf(sTaskType) > -1) { var sParentChild = this.getDynatraceApplicationOfServiceOffering(current); var sDynatraceApp = sParentChild.DynatraceApplication; //gs.log(current.number+':: DYNATRACE APP: ' + sDynatraceApp, 'scrpIncl:getConfigurationItemFilter'); if (sDynatraceApp != '') { var sChildCisArray = this.filterByService(sDynatraceApp, current.service_offering); gs.log('sChildCisArray=' + sChildCisArray, 'scrpIncl:getConfigurationItemFilter'); if (sChildCisArray.length > 0) { configItemFilter += '^sys_idIN'; configItemFilter += sChildCisArray; gs.log(current.number + ':: configItemFilter: ' + configItemFilter, 'scrpIncl:getConfigurationItemFilter'); } } } } } catch (ex) { } return configItemFilter; }, getDynatraceApplicationOfServiceOffering: function(current) { var oParentCIs = { "DynatraceApplication": "", "ServiceOffering": "" }; try { var grRel = new GlideRecord('cmdb_rel_ci'); //--dynatrace application table var sQuery = 'parent.sys_class_name=service_offering^child.sys_class_name=cmdb_ci_service_calculated^child.nameLIKEAPPLICATION-'; grRel.addEncodedQuery('sQuery'); grRel.addQuery('parent', current.service_offering); grRel.query(); if (grRel.next()) { oParentCIs.DynatraceApplication = grRel.child + ""; oParentCIs.ServiceOffering = grRel.parent + ""; } } catch (ex) { gs.log(current.number + ', ERROR:' + ex.toString(), 'scrIncl:getDynatraceApplicationOfServiceOffering'); } return oParentCIs; /*(var grDTApp=new GlideRecord('cmdb_ci_service_calculated'); //--dynatrace application table sQuery='discovery_source=SGO-Dynatrace^nameLIKEAPPLICATION-^operational_status=1' grDTApp.addEncodedQuery('sQuery'); grDTApp.query(); */ }, filterByService: function(dynatraceApplication, serviceOffering) {var custom_taskUtils = Class.create(); custom_taskUtils.prototype = { DEFAULT_CI_FILTER: gs.getProperty('custom_ci_filter'), TABLES_IN_SCOPE: 'incident,problem,change_request', //--can also leverage sys property 'com.snc.task.principal_class_filter'; initialize: function() {}, getConfigurationItemFilter: function(current) { if (!current) { gs.error("[TaskUtilsSNC.getConfigurationItemFilter] : Invalid parameter"); return; } var configItemFilter = this.DEFAULT_CI_FILTER; try { //gs.log(current.number + ':: SERVICE OFFERING: ' + current.service_offering + ', ' + current.service_offering.name, 'scrpIncl:getConfigurationItemFilter'); if (!gs.nil(current.service_offering)) { var sTaskType = current.getTableName(); if (this.TABLES_IN_SCOPE.indexOf(sTaskType) > -1) { var sParentChild = this.getDynatraceApplicationOfServiceOffering(current); var sDynatraceApp = sParentChild.DynatraceApplication; //gs.log(current.number+':: DYNATRACE APP: ' + sDynatraceApp, 'scrpIncl:getConfigurationItemFilter'); if (sDynatraceApp != '') { var sChildCisArray = this.filterByService(sDynatraceApp, current.service_offering); gs.log('sChildCisArray=' + sChildCisArray, 'scrpIncl:getConfigurationItemFilter'); if (sChildCisArray.length > 0) { configItemFilter += '^sys_idIN'; configItemFilter += sChildCisArray; gs.log(current.number + ':: configItemFilter: ' + configItemFilter, 'scrpIncl:getConfigurationItemFilter'); } } } } } catch (ex) { } return configItemFilter; }, getDynatraceApplicationOfServiceOffering: function(current) { var oParentCIs = { "DynatraceApplication": "", "ServiceOffering": "" }; //--for example, if service offering selected is 'Divorce' //https://customtemp.service-now.com/cmdb_rel_ci_list.do?sysparm_query=parent%3D8aee2749db7a9300ba27aa484b961968%5EORparent%3Da9de62f81b3edd106a892131b24bcb8d&sysparm_view= try { var grRel = new GlideRecord('cmdb_rel_ci'); //--dynatrace application table var sQuery = 'parent.sys_class_name=service_offering^child.sys_class_name=cmdb_ci_service_calculated^child.nameLIKEAPPLICATION-'; grRel.addEncodedQuery('sQuery'); grRel.addQuery('parent', current.service_offering); grRel.query(); if (grRel.next()) { oParentCIs.DynatraceApplication = grRel.child + ""; oParentCIs.ServiceOffering = grRel.parent + ""; } } catch (ex) { gs.log(current.number + ', ERROR:' + ex.toString(), 'scrIncl:getDynatraceApplicationOfServiceOffering'); } return oParentCIs; /*(var grDTApp=new GlideRecord('cmdb_ci_service_calculated'); //--dynatrace application table sQuery='discovery_source=SGO-Dynatrace^nameLIKEAPPLICATION-^operational_status=1' grDTApp.addEncodedQuery('sQuery'); grDTApp.query(); */ }, filterByService: function(dynatraceApplication, serviceOffering) { var retArray = []; var relGr = new GlideRecord('cmdb_rel_ci'); var qString = 'parent=' + dynatraceApplication + '^ORparent=' + serviceOffering; relGr.addEncodedQuery(qString); relGr.query(); while (relGr.next()){ var grCI=new GlideRecord('cmdb_ci'); if (grCI.get('sys_id', relGr.child)){ if (grCI.sys_class_name != 'cmdb_ci_service_calculated') { retArray.push(grCI.sys_id.toString()); } } } return retArray; }, type: 'custom_taskUtils' }; var retArray = []; var relGr = new GlideRecord('cmdb_rel_ci'); var qString = 'parent=' + dynatraceApplication + '^ORparent=' + serviceOffering; relGr.addEncodedQuery(qString); relGr.query(); while (relGr.next()){ var grCI=new GlideRecord('cmdb_ci'); if (grCI.get('sys_id', relGr.child)){ if (grCI.sys_class_name != 'cmdb_ci_service_calculated') { retArray.push(grCI.sys_id.toString()); } } } return retArray; }, type: 'custom_taskUtils' };

Comments

Popular posts from this blog

GlideRecord setValue

URL link in addInfoMessage