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

URL link in addInfoMessage

var ga=new GlideAjax('gld_HR_ajax'); ga.addParam('sysparm_name', 'checkEmployeeNumber_hrProfile'); ga.addParam('sysparm_hrprofilenumber', g_form.getValue('number')); ga.addParam('sysparm_employeenumber', newValue); ga.getXMLAnswer(function(answer) { if (answer!='undefined' && answer!=''){ var navURL="<a style='text-decoration:underline;color:blue' href=hr_profile.do?sysparm_query=number=" + answer + ">" + answer + "</a><img width='3' src='images/s.gif'/>"; var sMsg='The employee number entered already exists on another HR Profile ' + navURL; //alert(sMsg); g_form.showErrorBox('employee_number', 'error - please check'); g_form.addInfoMessage(sMsg); } });

GlideRecord setValue

setValue(String name, Object value) Sets the specified field to the specified value. Normally a script would do a direct assignment, for example,  gr.category = value . However, if in a script the element name is a variable, then  gr.setValue(elementName, value)  can be used. When setting a value, ensure the data type of the field matches the data type of the value you enter. This method cannot be used on journal fields. If the value parameter is null, the record is not updated, and an error is not thrown https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=r_GlideRecord-setValue_String_Object