How to populate custom 'Template' field on incident table with name of template applied

jas101
Tera Expert

Hi guys, it's my understanding that's it's still not possible to easily track/monitor template usage.

If this is correct I figure adding u_template field to incident table and getting the value of applied template in said field is best way to do this - I've seen the SNGuru article https://www.servicenowguru.com/system-definition/advanced-templates/ but if I'm honest still not sure what the best/most lightweight option is to parse this value? Does it need to be a client script or can it be a business rule?

@Allen A - maybe you're the man to help again with this one! Thanks.

7 REPLIES 7

Hi,

For brand new template to be auto-filled...you could do something like an onLoad/onChange client script (so the example below works for both onLoad and onChange) for when the form loads or the "table" field is selected. So let's say they picked the incident table and I wanted to set a template field to something automatically, this would be a sample script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (newValue === '') {
        return;
    }
    if (g_form.getValue('table') == 'incident') {
		g_form.setValue('template', 'active=true');
	}
}

So template for population via script uses what's considered EncodedQuery. So you can populate MANY fields by adding more to this encodedquery. As far as hiding that you did it, I'm not sure about that because the template section within this form, is really like one long query that you're building and so I don't think you can hide particular pieces.

So in another example, if I wanted to jumpstart the template form for the person creating it, I could do via script where I want active is true, assignment group is service desk and incident state is in progress (just as an example).

This would be the script for that:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (newValue === '') {
        return;
    }
    if (g_form.getValue('table') == 'incident') {
        g_form.setValue('template', 'active=true^assignment_group=d625dccec0a8016700a222a0f7900d06^incident_state=2');
    }
}

As far as going back and correcting records that don't have the template field filled in already, you could populate that via script. First you'd need to accurately identify the records that were made with a template that you know for sure.

Then, depending on how you found them, you'd create a gliderecord query (this can be done in background script OR fix script if adding to an update set to promote to another environment for use there too). https://www.servicenowguru.com/scripting/gliderecord-query-cheat-sheet/

You'd need to grab the sys_id of your template (you can navigate to your sys_template list of records and right-click the template and copy the sys_id):

find_real_file.png 

Then your gliderecord query could look like:

var gr = new GlideRecord('incident');
gr.addEncodedQuery('sys_created_onONLast 30 days@javascript:gs.beginningOfLast30Days()@javascript:gs.endOfLast30Days()');
gr.query();
while(gr.next()) {
gr.u_template = 'sys_id_of_template_paste_here';
gr.update();
}

So this is just an example script you can use, but that was to find all records created in the last 30 days. I made a guess that these were all made with a template, we just didn't capture it because the field didn't exist then, but now it does, so now I want it populated. Replace u_template, with the name of your custom template field.

Oh and one final thing, if you're wondering about encoded query or how to grab the encoded query...you can navigate to your table list of records, so like...incidents, use the filter to filter accordingly, then simply right-click the last piece of the breadcrumb trail and choose 'copy query':

find_real_file.png

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi,

I just wanted to check in on this. If my reply helped guide you correctly, please mark it as Helpful & Correct.

Thank you!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Casper6060
Mega Sage

To anyone else looking into this.

A good way around this is to create an on before business rule that runs on insert and update and then check the template record, if it contains a field called u_template, that has a value, then thats good, if not it will have to be added.

 

Here is an example of a working script that utilizes the u_template field:

 

var table = current.getValue('table');
    var gr = new GlideRecord(table);
    if (gr.instanceOf('task')) {
        var newTemplate = current.getValue('template');
        if (newTemplate) {
            // Remove existing u_template field and add it back
            newTemplate = newTemplate.replace(/\^EQ$/, '');
            newTemplate = newTemplate.replace(/\^u_template=\w*/, '');
            newTemplate += '^u_template=' + current.getUniqueValue();
        } else {
            // Template was empty
            newTemplate = 'u_template=' + current.getUniqueValue();
        }
        newTemplate += '^EQ';
        current.setValue('template', newTemplate);
    }

 

 
Hope this helps