Blog

Apply Template on Field Change

Use a client script to automatically apply a template if a field changes.  Here is an example client script:

Name: Password Reset Auto Template
Active: True
Global: True
Type: onChange
Table: Incident
Field name: subcategory
Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(newValue == 'Password Reset' && g_form.getValue('category') == 'Self-Service'){
var grTemplate = new GlideRecord('sys_template');
grTemplate.addQuery('name','Incident Password Reset');
grTemplate.query(templateResponse);
}

function templateResponse(template) {
if (template.next()) {
applyTemplate(template.sys_id);
}
}
}