Skip to main content

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

Get URL Parameter - server side script (portal or classic UI)

Classic UI : var sURL_editparam = gs . action . getGlideURI (). getMap (). get ( ' sysparm_aparameter ' ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); } Portal : var sURL_editparam = $sp . getParameter ( " sysparm_aparameter " ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); }

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){