Skip to content

Instantly share code, notes, and snippets.

@3en
Created December 6, 2012 16:15
Show Gist options
  • Save 3en/4225690 to your computer and use it in GitHub Desktop.
Save 3en/4225690 to your computer and use it in GitHub Desktop.
Zendesk - Classic - Incident Tagging
// Flag Incident Type
// Author: Ben Matthew Reyes - EMEA - [email protected]
// Written: December 2012
// Usable by any customers using the classic interface.
// Screencast - http://www.screenr.com/8Cx7
// --- Install Instructions ---
// Add as a global javascript widget, visible to users that are logged in and are agents.
// Change the two below variables to suit your requirements. (Optional)
// --- Editable Variables --- //
var incidentTag = 'problem'; // global incident tag (applies to all incident)
var prependIncidentTag = 'problem_'; // prepend incident link ID tag
if ($j('body.tickets').is(':visible')) {
$j(document).ready(function(){
$j('#ticket_ticket_type_id').change(function(){
if ($j("#ticket_ticket_type_id").val() === "2") {
bindIncidentTagging();
} else {
removeIncidentTags();
}
});
});
$j(window).load(function() {
bindIncidentTagging();
});
// Binds the functions to check the incident checkbox when incident is selected
function bindIncidentTagging() {
addIncidentTags();
$j('#ticket_link').change(function(){
addIncidentTags();
});
}
function addIncidentTags() {
if ($j('#ticket_link').is(':visible')) {
var ticketLinkVal = $j('#ticket_link option:selected').html();
if (ticketLinkVal !== '') {
var incidentID = "";
// Regular Expression match for ticketID
var regExMatch = ticketLinkVal.match(/^\#(\d+)/);
if (regExMatch.length > 0) {
incidentID = ticketLinkVal.match(/^\#(\d+)/)[1]
var incidentLinkTag = prependIncidentTag + incidentID;
ticketTagField.addEntry(incidentLinkTag);
// Loop through ticket tags and remove tags that are prepended with incident label *but not the new added tag*
var currentTags = $j(ticketTagField.holder).find('li input').map(function(x, y) { if($j(y)) return $j(y).val(); });
for (var i=0; i<currentTags.length; i++) {
if (incidentLinkTag !== currentTags[i]) {
var re = new RegExp("^"+prependIncidentTag, "g");
if (re.test(currentTags[i])) {
ticketTagField.removeEntry(currentTags[i]); // remove linked ID incident tag
}
}
}
}
ticketTagField.addEntry(incidentTag);
} else {
removeIncidentTags();
}
}
}
function removeIncidentTags() {
ticketTagField.removeEntry(incidentTag); // remove global incident tag
// Loop through ticket tags and remove tags that are prepended with incident label
var currentTags = $j(ticketTagField.holder).find('li input').map(function(x, y) { if($j(y)) return $j(y).val(); });
for (var i=0; i<currentTags.length; i++) {
var re = new RegExp("^"+prependIncidentTag, "g");
if (re.test(currentTags[i])) {
ticketTagField.removeEntry(currentTags[i]); // remove linked ID incident tag
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment