- 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:40 PM
The article describes a certain use case using the applyTemplate. They explain why they create a field and clear the value. But it is not needed. You are free to use applyTemplate without the rest of the code if it better fits your requirement.