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

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