Every now and then we get a customer question if it is possible to report on the use of templates. Usage like the number of times templates are used and by whom.
Tracking this has become a bit more challenging since the ServiceNow has locked down the code of the template bar functionality. The following solution is not 100% bulletproof. You probably should check for multiple occasions of the class ‘outputmsg_text’ but it should give you a good idea of what is possible). Also for demonstration purpose I made use of a synchronous GlideRecord query which you should always avoid (so no need to leave comments for that).
1. Create a new reference field ‘u_template’ (reference table = sys_template) on the incident form.
2. Create a new client script with the following code:
function onSubmit() {
//Type appropriate comment here, and begin script below
try {
var template_html = $j(".outputmsg_text").html().split("<\br>")[0];
//alert("template_html [" + template_html + "]");
var patt = new RegExp("(.*?) Template Applied");
var template = patt.exec(template_html);
alert("template [" + template[1] + "]");
var rec = new GlideRecord('sys_template');
rec.addQuery('name', template[1]);
rec.query();
if (rec.next()) {
alert("template name/sys_id [" + rec.name + "] [" + rec.sys_id + "]");
g_form.setValue('u_template',rec.sys_id);
}
} catch(err) {
}
}

