How to copy the sysid of a template to u_template in a new change request form

HugoFirst
Kilo Sage

I'm working on a customization and I'm very close, ( I think! ) but there is a roadblock I'd like some help with.

In this post, I'll briefly describe my use case, then I'll (briefly!) describe my implementation and then I'll describe my roadblock.

My use case:

A user opens a new change request form, sets the type to standard and then selects a item from the standard change list and saves the form. Then the user right clicks and selects a menu option to "Apply Template" and the template associated with the standard change list item is applied to the change request.

My implementation:

I have a custom table "Standard Change List Item" which is populated with about 100 items ( and it's growing ).

One field in the "Standard Change List Item" table is u_template, which points to the sys_template table.

When we define a standard change   list item, we select a template to be associated with that item.

All this appears to work fine.   Now for the Change Request


I added the Template field to the Change Request form, though it does NOT actually display.

On the change_request form, I have an OnChange client script "Apply Standard Change Item" which copies the item's u_template field to the g_form's u_template field.   This client script fires anytime the standard change item is changed on the form.


My Road Block is this:

The change request form behaves as if the u_template field is DOES NOT EXIST.

I use alerts in my client script to "Apply Standard Change Item" to show that it runs when a standard change list item is selected.

It displays a nice sysid for the item's template field. But no value has ever been displayed for the change request form's u_template field.

Needless to say, the change request record never receives the sysid of the template in its u_template field.

And so no template is applied to the change request when I click "Apply template".


I'm hoping that there's an obvious solution for this that I'm too close to the forest to see the trees.


References I've checked to date:

http://wiki.servicenow.com/index.php?title=Creating_a_Template

http://www.servicenowguru.com/system-definition/advanced-templates/

Choose template from dropdown?

Thanks in advance for any help you can lend.

5 REPLIES 5

Michael Fry1
Kilo Patron

The u_template field isn't on the form. What if you add it to the form, does everything work?


The variable is on the form.   But I'm making progress.   I learned that the u_template field is populated from my UI action.


Then something surprising happened.   A client script on the task table, named Apply Template,   executes when the u_template field is changed. It attempts to apply the template.   And it almost works.   The change request takes on the values in the template record.   But child change tasks are not created.   The last step of the client script is to set u_template to a null value.   Hence my previous mistaken belief that the field was not being set.   I wanted to see if this client script is OOB, but I can't seem to find the Demo site anymore to check.



Here's another helpful doc that I found:


Creating a Template for Change Request Tasks - ServiceNow Wiki



This doc leads me to believe that my use case is not supported without reimplementing the "apply template" function, so I'll meet with the user and see if we can revise it.


Update:



We succeeded in getting templates to work as documented in the wiki article.


This does not support our use case however.   So we are going in another direction where we will use existing change requests as our template and will duplicate the information and tasks to a current change request through the use of a UI Action.


The field is on the form and it turns out to work in a sense, just not how I had hoped for my use case.


The issue is an "On change" client script ( ApplyTemplate ) which runs when the u_template field is changed.


It immediately tries to apply a template and then it clears the u_template variable to null.


My initial interpretation of this was that the variable was not being set.   But I was mistaken. It was being set, for several milliseconds.   Then it was reset.



My solution is to implement a different set of logic so that I can employ templating functionality that meets my use case.