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

ServiceNow timeout settings for virtual agent chat

  After connecting to live agent User is connected to live agent and not responding to live agent chat or distracted. The below properties controls the timeout settings, the OOB settings for reminder is [180 sec] and for cancelling the chat is [360 sec]. The job is default configured to 2 min so I believe no tweaking is required here.  Property -   com.glide.cs.idle_chat_reminder_timeout                  com.glide.cs.idle_chat_cancel_timeout Scheduled job   - Idle Chat Timer Task   https://community.servicenow.com/community?id=community_article&sys_id=1453b03bdbaad0109e691ea668961929   (ServiceNow ) 

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 )){...