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

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

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.

cbarlock
Kilo Guru

Thanks!  Is applyTemplate a built in function?  Why clear the template?

Yes, applyTemplate is available.

I tested this (on an Incident) with the sys_id of a template and that works:

applyTemplate("acd87481d7930200f2d224837e6103f3")

find_real_file.png

Sys_id is from the Incident Call Type template. The script applied it.