Apply Template to Specific Field within Form

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2017 08:38 AM
Hi,
I am VERY new to ServiceNow and JavaScript so bear with me.
I am trying to apply a template to the 'Description' field within my form. I need the template to appear when the user clicks into the field so that they can fill out more specific information about their incident.
I have this so far:
function onLoad() {
if(g_form.isNewRecord()){
applyTemplate('gm_story');
}
}
How do I add this template to the description field specifically and how do I get it to appear when the user clicks in to it?
Thank you,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-01-2017 09:50 PM
Hey Casey,
Not sure if you ever got this working, but I see you were trying to do this as an onload (which happens when form loads), but I am not sure whether an onclick event would be the best solution here either.
If you are looking to load a template on load though, see below for a script free method (from http://wiki.servicenow.com/index.php?title=Creating_a_Template#gsc.tab=0 (section 6):
You can make a template that automatically applies to new, user-created records. To create this kind of template, set the template Name to match the name of the table the template applies to. This type of template ignores the User and Group fields on the Template form. Automatic templates are always global. The template does not apply to records created through an automated process such as a business rule, UI action, or workflow.
For example, a template called cmdb_ci_win_server with a Table value of Windows Server [cmdb_ci_win_server] is always applied when a user creates a new windows server record.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-16-2020 06:11 AM
This is an old post but I referenced it when searching for something else and noticed (A.) what might have been the original problem and (B.) a few other suggestions for the scenario you detailed.
Firstly the suggestions already made to use the OOTB method to apply a default template to a form is great if you can (*but some tables don't allow this like kb_knowledge), also see my alternative suggestions as templates are great for some things but there are a lot of other options for directing a user to populate the right info in a form or pre=populating a single field.
What might fix your code:
- Did you try using the sys_id of the template instead of the name? - That works for me.
- I would recommend you have something in the code to check if the target field has already been populated, otherwise you'll be wiping existing data every time the form loads for a new record this will be annoying when creating incidents from other processes when the description will no doubt be auto populated from the source record.
function onLoad() {
// check if the form is a new and checks if description is blank
if(g_form.isNewRecord() && g_form.getValue('description') == ''){
// replace <sys_id> below with the sys_id of your preferred sys_template record
applyTemplate('<sys_id>');
}
}
Some Alternatives:
Few thoughts on some alternative ways to achieve this:
- Set a default - You could just populate the default value of the field
- Script a default - If for some reason 1. is a non starter then you could get the script to set the default field value and by pass the template
- Alternatives to setting a default on such a high value field - adding default content to a field will make it difficult to ensure the user actually updates it before submitting the form. To combat this you could consider:-
- Record Producer with Example Text
Would it be better to direct less experienced users to a record producer where you can add "Example Text" to the variables, which will appear in grey when the field is empty? This would allow you to suggest what the user should be entering without actually populating the field
. . . also catalog items allow for help text, tool tips and instructions to be attached to each Question so very versatile. - More specific fields if specific detail is needed
If you need specific data to be captured on the form consider introducing additional targeted fields as these will allow for better data utilisation when processing the incident. - Hints, Field Messages and Labels with hyperlinks to Knowledge
If not using a catalog item/record producer you could add a hint to the label or if you want it more prominent a message to the field while the field is empty advising what the content should be and it's importance. You can even have a link embedded in the label that can point to a knowledge article giving more details if the message would be too wordy - Guided Tour
You could also consider a Guided Tour for the incident page that would trigger the first time users load the incident form that would advise what should be populated in the field, this will then stop existing users being nagged repeatedly.
- Record Producer with Example Text