Skip to main content

ServiceNow ARRAYS: Group matching incidents by CI and short description (typically from alerts) into a problem record

 (ServiceNow )

sample script - could be moved into a monthly scheduled job script

scheduled job code (calls the script include): 

var si=new hmcts_dynatraceINCtoPRB().mergeDynatraceIncidentsIntoProblems(false);




in this example, the short description from the dynatrace alert is populated in the incident like so 

element 1 | element 2 | element 3 | element 4 (element 4 is not checked)

script include code
var dynatraceINCtoPRB = Class.create(); dynatraceINCtoPRB.prototype = { initialize: function() { this.bDebug = false; this.bUpdate = true; this.encodedQuery = gs.getProperty('dynatrace.prb_automation_mgmt.encoded_query'); //this.bUpdate=false; //--testing only }, mergeDynatraceIncidentsIntoProblems: function(bDebug) { if (bDebug) { this.bDebug = true; } var grDTincs = new GlideAggregate('incident'); grDTincs.addQuery('correlation_displayLIKEdynatrace'); grDTincs.addQuery('cmdb_ciISNOTEMPTY'); grDTincs.addQuery('problem_id', ''); grDTincs.addEncodedQuery(this.encodedQuery); grDTincs.query(); var dt_inc_arr = []; while (grDTincs.next()) { var sCI = grDTincs.cmdb_ci; var sSD = grDTincs.short_description.substring(0, grDTincs.short_description.lastIndexOf('|') + 1); sSD = sSD.substring(0, sSD.length - 1); //--trim the last "|" dt_inc_arr.push(sCI + ';' + sSD); } dt_inc_arr.sort(); //gs.print(dt_inc_arr); this._count_duplicate(dt_inc_arr); return "COMPLETE"; }, _count_duplicate: function(a) { var counts = {}; var bBreak = false; //bBreak = true; //--testing only, to allow for testing of one batch of incidents only var iDuplicateThreshold = parseInt(gs.getProperty('dynatrace.prb_automation_mgmt.incident_threshold')); for (ic = 0; ic < a.length; ic++) { if (counts[a[ic]]) { counts[a[ic]] += 1; } else { counts[a[ic]] = 1; } } for (prop in counts) { if (counts[prop] >= iDuplicateThreshold) { if (this.bDebug) { gs.log(prop + " counted: " + counts[prop] + " times.", "SIDTINCPRB:forLoop"); } try { this._incToPrb(prop); //--link the incidents to problem } catch (ex) { gs.logError('ERROR: ' + ex.toString, 'SIDTINCPRB:_count_duplicate'); } if (bBreak) { gs.log('BREAK statement enabled', 'SIDTINCPRB:_count_duplicate'); break; } } } //gs.print(counts); }, _incToPrb: function(qString) { var sArr = qString.split(';'); var grDTincs = new GlideRecord('incident'); if (!gs.nil(sArr[0]) && !gs.nil(sArr[1])) { grDTincs.addQuery('cmdb_ci', sArr[0]); grDTincs.addQuery('short_descriptionSTARTSWITH' + sArr[1]); grDTincs.addQuery('correlation_displayLIKEdynatrace'); grDTincs.addEncodedQuery(this.encodedQuery); grDTincs.addQuery('problem_id', ''); grDTincs.query(); while (grDTincs.next()) { var sPrbID = this._getExistingProblemRecord(sArr[0], sArr[1]); if (sPrbID != '') { grDTincs.problem_id = sPrbID; } else { if (this.bUpdate) { sPrbID = this._setNewProblemRecord(sArr[0], sArr[1]); grDTincs.problem_id = sPrbID; } } if (this.bUpdate) { grDTincs.update(); } if (!gs.nil(sPrbID)) { gs.log('Incident ' + grDTincs.number + ' linked to problem ' + sPrbID, 'SIDTINCPRB:_incToPrb'); } } } }, _getExistingProblemRecord: function(cmdb_ci, short_description) { var sPrbID = ''; var existing_problem = new GlideRecord('problem'); existing_problem.addQuery('problem_id', 'NOT IN', '4, 11'); existing_problem.addQuery('cmdb_ci', cmdb_ci); existing_problem.addQuery('short_description', short_description); existing_problem.query(); if (existing_problem.next()) { sPrbID = existing_problem.sys_id; gs.log('Existing problem ' + existing_problem.number + ' in place for common Dynatrace Incidents', 'SIDTINCPRB:_getExistingProblemRecord'); } return sPrbID; }, _setNewProblemRecord: function(cmdb_ci, short_description) { var sPrbID = ''; var new_problem = new GlideRecord('problem'); new_problem.newRecord(); new_problem.short_description = short_description; new_problem.assignment_group = gs.getProperty('dynatrace.prb_automation_mgmt.assignment_group'); new_problem.cmdb_ci = cmdb_ci; sPrbID = new_problem.insert(); gs.log('New problem ' + new_problem.number + ' created for common Dynatrace Incidents', 'SIDTINCPRB:_setNewProblemRecord'); return sPrbID; }, type: 'dynatraceINCtoPRB' };

Comments

Popular posts from this blog

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

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 ); }