- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2021 02:12 PM
Is it possible to programmatically apply a template to a form, say, in an onChange script when a value is selected from a choice list? Google was spectacularly unhelpful answering this question!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2021 02:25 PM
Yes, you can use applyTemplate("template name") (template name or sys_id between the brackets) in your onChange client script.
There has been a Servicenowguru article around for a while which stores the template in a field and applies that:
https://servicenowguru.com/system-definition/advanced-templates/
function onChange(control, oldValue, newValue, isLoading) {
if(isLoading)
return;
try{
var template = g_form.getValue('u_template');
if(template != ''){
//Apply the selected template
applyTemplate(template);
//Wait for the template to be applied and then clear the template field
window.setTimeout(clearTemplate,2000);
}
}
catch(e){
//alert(e);
}
}
function clearTemplate(){
g_form.setValue('u_template', '');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2021 02:25 PM
Yes, you can use applyTemplate("template name") (template name or sys_id between the brackets) in your onChange client script.
There has been a Servicenowguru article around for a while which stores the template in a field and applies that:
https://servicenowguru.com/system-definition/advanced-templates/
function onChange(control, oldValue, newValue, isLoading) {
if(isLoading)
return;
try{
var template = g_form.getValue('u_template');
if(template != ''){
//Apply the selected template
applyTemplate(template);
//Wait for the template to be applied and then clear the template field
window.setTimeout(clearTemplate,2000);
}
}
catch(e){
//alert(e);
}
}
function clearTemplate(){
g_form.setValue('u_template', '');
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2022 06:54 PM
Interesting, I know I'm necro-posting here, but I've seen this article referenced a few times on community and I wonder why they went with this approach. For applying a template completely from a client machine, it seems like you'd be able to do the following entirely via client script:
- Check if the logged in user can access the sys_template table (which they should by default)
- Check if the logged in user can access the requisite template record
- Read the template field on the requisite template record
- Parse the template field into a useable format i.e. string to an array
- Check for the presence of fields in the array
- Apply values from the array to the form
- Generate any errors if required
I'm happy to hear any ideas as to why it may not work, however it seems on the face of it that it should. This also would negate the need to add an additional field to the form and not rely on adding a delay.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2021 02:31 PM
Thanks! Is applyTemplate a built in function? Why clear the template?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2021 02:37 PM