|
| 1 | +(function() { |
| 2 | + |
| 3 | + var THRESHOLD = 4; // min number of similar incidents |
| 4 | + var DAYS = 7; |
| 5 | + |
| 6 | + |
| 7 | + var incGR = new GlideAggregate('incident'); |
| 8 | + incGR.addEncodedQuery('active=true^opened_atRELATIVEGT@dayofweek@ago@' + DAYS); //Query recent incidents |
| 9 | + incGR.groupBy('short_description'); |
| 10 | + incGR.addAggregate('COUNT'); |
| 11 | + incGR.query(); |
| 12 | + |
| 13 | + while (incGR.next()) { |
| 14 | + var count = parseInt(incGR.getAggregate('COUNT'), 10); |
| 15 | + var desc = incGR.short_description.toString(); |
| 16 | + |
| 17 | + //Checking for high repetition |
| 18 | + if (count >= THRESHOLD) { |
| 19 | + |
| 20 | + //Avoid duplicate problem |
| 21 | + var existingProb = new GlideRecord('problem'); |
| 22 | + existingProb.addQuery('short_description', 'CONTAINS', desc); |
| 23 | + existingProb.addQuery('active', true); |
| 24 | + existingProb.query(); |
| 25 | + |
| 26 | + if (!existingProb.hasNext()) { |
| 27 | + |
| 28 | + //if not exist creating Problem Record |
| 29 | + var prob = new GlideRecord('problem'); |
| 30 | + prob.initialize(); |
| 31 | + prob.short_description = "Recurring Incident Pattern: " + desc; |
| 32 | + prob.description = "Auto-generated problem for " + count + |
| 33 | + " similar incidents in the last " + DAYS + " days.\n\nExample Description:\n" + desc; |
| 34 | + prob.u_detected_by = 'System'; |
| 35 | + prob.state = 1; |
| 36 | + var probSysId = prob.insert(); |
| 37 | + |
| 38 | + gs.info(" Created new Problem [" + prob.number + "] for repeated incidents (" + count + "): " + desc); |
| 39 | + |
| 40 | + //link incident with problem |
| 41 | + var incLinkGR = new GlideRecord('incident'); |
| 42 | + incLinkGR.addQuery('short_description', desc); |
| 43 | + incLinkGR.addQuery('active', true); |
| 44 | + incLinkGR.query(); |
| 45 | + while (incLinkGR.next()) { |
| 46 | + incLinkGR.problem_id = probSysId; |
| 47 | + incLinkGR.update(); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +})(); |
0 commit comments