Programmatically Apply a Template To a Form?

cbarlock
Kilo Guru

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!

1 ACCEPTED SOLUTION

Willem
Giga Sage
Giga Sage

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', '');
}

View solution in original post

5 REPLIES 5

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.