Auto Tag Assignment group based on short description and assignment group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2024 11:26 PM
When an incident is created, we need to check the short description for keywords and the assignment group. We should then look up these keywords in the label table. If we find matched labels with the short description keywords, we need to ensure that the incident assignment group matches the label's assigned group. Only labels with matching groups should be filtered. If a label does not have an assigned group, it can be ignored. The matched labels should then be inserted as tags for that incident in the label_entry table. The requirement is to auto-assign tags, when they are first created, based on the presence of specific keywords in Short description. One group may have more than one label. incidents.
For that I have created an after insert or update BR:
(function() {
// common words to ignore
var commonWords = ['in', 'at', 'from', 'the', 'and', 'of', 'a', 'to', 'for', 'with', 'on', 'by', 'as', 'or', 'an', 'but', 'is', 'are', 'was', 'were', 'will', 'shall', 'should', 'would', 'could', 'this', 'that', 'which', 'who', 'whom', 'whose', 'where', 'when', 'why'];
// This Function is to extract keywords and phrases from a short description
function extractKeywordsAndPhrases(description) {
var keywords = [];
var words = description.toLowerCase().split(/\s+/);
// This loop is to Extract single keywords
for (var i = 0; i < words.length; i++) {
var word = words[i].replace(/[^a-z0-9]/g, ''); // Remove punctuation
if (word.length > 2 && commonWords.indexOf(word) === -1) {
keywords.push(word);
}
}
// This loop is to Extract multi-word phrases (up to 3 words) as additional keywords
for (var j = 0; j < words.length - 1; j++) {
var phrase = words[j] + ' ' + words[j + 1];
if (phrase.length > 5 && commonWords.indexOf(words[j]) === -1 && commonWords.indexOf(words[j + 1]) === -1) {
keywords.push(phrase);
}
}
return keywords;
}
// Extracing keywords and phrases from the short description
var keywords = extractKeywordsAndPhrases(current.short_description);
gs.addInfoMessageMessage("Keywords and phrases extracted: " + keywords.join(", "));
// Getting the assignment group associated with the current incident
var assignmentGroup = current.assignment_group;
if (!assignmentGroup) {
gs.addInfoMessageMessage("No assignment group set for the incident.");
gs.addInfoMessageMessage("Exiting script as there is no assignment group.");
return; // Early exit from the function
}
// This Function is to query labels based on a keyword or phrase and match assignment group
function queryLabels(searchTerm, groupSysId) {
var label = new GlideRecord('label');
label.addQuery('name', 'CONTAINS', searchTerm);
label.query();
var matchedLabels = [];
while (label.next()) {
// Checking if the label's group_list includes the assignment group
var groupList = label.group_list;
if (groupList && groupList.indexOf(groupSysId) !== -1) {
matchedLabels.push(label.getValue('sys_id'));
gs.addInfoMessageMessage("Matched label: " + label.name + " with sys_id: " + label.sys_id);
}
}
return matchedLabels;
}
// Collecting all matched labels
var allMatchedLabels = {};
for (var j = 0; j < keywords.length; j++) {
var keyword = keywords[j];
gs.addInfoMessageMessage("Searching labels for keyword: " + keyword);
var matchedLabels = queryLabels(keyword, assignmentGroup);
for (var k = 0; k < matchedLabels.length; k++) {
allMatchedLabels[matchedLabels[k]] = true; // Add all matched labels
}
}
// Checking if any labels were found for the assignment group
if (Object.keys(allMatchedLabels).length === 0) {
gs.addInfoMessageMessage("No matching labels found for the assignment group.");
return; // Early exit from the function
}
//To avoid adding duplicate label entries
var existingLabels = {};
var labelEntry = new GlideRecord('label_entry');
labelEntry.addQuery('table', 'incident');
labelEntry.addQuery('table_key', current.sys_id);
labelEntry.query();
while (labelEntry.next()) {
existingLabels[labelEntry.label] = true;
}
// Inserting label entries for all matched labels
for (var labelSysId in allMatchedLabels) {
if (!existingLabels[labelSysId]) { // Check if label is already added
var gr1 = new GlideRecord('label_entry');
gr1.initialize();
gr1.id_display = current.number;
gr1.label = labelSysId;
gr1.title = "Incident - " + current.number;
gr1.table = 'incident';
gr1.table_key = current.sys_id;
gr1.read = 'yes';
gr1.url = 'incident.do?sys_id=' + current.sys_id + '&sysparm_view=';
gr1.insert();
// Logging for debugging
gs.addInfoMessage("Inserted label entry for label sys_id: " + labelSysId);
}
}
// Log completion
gs.addInfoMessage("Completed processing labels for incident: " + current.number);
})();
Issue: There are 4 labels matched but only two were getting inserted as tags but all 4 labels were inserted in label_entry table.
Finding: For example if it finds two labels with the name gift then it is inserting only the last label as a tag and first one ignored, If it finds two labels with test then it is inserting only the last label as a tag and first one ignored.