(function() {
// CONFIG: point this at your own template
var TEMPLATE_SYS_ID = 'REPLACE_WITH_TEMPLATE_SYS_ID'; // 32-char sys_id
var TEMPLATE_NAME = 'My Template'; // display label
if (!/^[a-f0-9]{32}$/i.test(TEMPLATE_SYS_ID)) {
alert('Set TEMPLATE_SYS_ID to a valid 32-char sys_id first.');
return;
}
var token = window.g_ck || document.querySelector('input[name="sysparm_ck"]')?.value || '';
var xhr = new XMLHttpRequest();
xhr.open('GET', '/api/now/table/sys_template/' + TEMPLATE_SYS_ID, false);
xhr.setRequestHeader('Accept', 'application/json');
xhr.setRequestHeader('X-UserToken', token);
xhr.send();
var result = JSON.parse(xhr.responseText).result;
if (!result || !result.template) { alert('Template not found!'); return; }
// Apply each field defined in the template string
result.template.split('^').forEach(function(field) {
var parts = field.split('=');
if (parts.length >= 2) {
var name = parts[0].trim();
var value = parts.slice(1).join('=').trim();
try { g_form.setValue(name, value); } catch (e) {}
}
});
// Optional: stamp the applied-template reference field (if you use one)
try { g_form.setValue('u_applied_template', TEMPLATE_SYS_ID, TEMPLATE_NAME); } catch (e) {}
})();
Apply a Template
Hint: Fetch a sys_template by sys_id and apply its fields to the current form
Trigger: \applytemplate