Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Mass Update Templates

anfield
Tera Guru

Is there a way to mass update templates on sys_template, through script? Need to do 2 things

 

1. Update a field on the template, template_used, which points to the template name on the template form

2. Update another field, template_applied_by, which contains - javascript:gs.getUserID()

The above two fields are both reference fields.

Purpose being when a template is applied to an incident, we can see who applied and which template

Have already reviewed the below URL but cant seem to get the syntax right

https://community.servicenow.com/community?id=community_question&sys_id=60e14ba9db98dbc01dcaf3231f961977

1 ACCEPTED SOLUTION

Okay, this script should work.  Just make sure that you adjust your 'addQuery' lines to only return the templates you actually want to update.  I modified this one so that it ignores individual templates and only updates Global and Group templates.  Once you're returning the right amount of templates you can un-comment the 'update()' line at the end to execute the update.

var encodedString = '^u_template_applied_by=javascript:gs.getUserID()^u_template_used=';
var grTem = new GlideRecord('sys_template');
//grTem.addQuery('sys_id', '692c6a13dbd4ef0048f171efbf961983'); // Add more query lines to restrict just to templates that need updating
grTem.addQuery('global', true).addOrCondition('group', '!=', '');
grTem.query();
gs.print(grTem.getRowCount()); // Print out the number of records being updated
while (grTem._next()) {
gs.print(grTem.template);
    grTem.template = grTem.template.replace('^EQ', '') + encodedString + grTem.name.toString() + '^EQ';
    grTem.setWorkflow(false); // Don't run business rules, etc.
    grTem.autoSysFields(false); // Don't update system fields
    //grTem.update(); // Un-comment this line once you're sure the row count is just the records you want to update.
}

View solution in original post

17 REPLIES 17

So are you trying to replace a value already in the template, or add new values to the template?  You'll want to use the script I gave you as your starting point, but replacing values vs. adding values will change what we do in the script.

Add new values. I just tried the above as someone else had posted a similar example on the communities under the url I originally added

Do you have an idea of what might work in the script

I have some idea, but it's impossible to know without seeing your templates.  That's why I suggested you set up one template exactly as you want, and then run a script to give you that template value.  You could run this from 'Scripts -> Background' to print out the value from a working template.  Just add your working template sys_id to the script and then copy the value it prints out to paste back here.

// Query for working template by sys_id
var t = new GlideRecord('sys_template');
t.addQuery('sys_id', 'YOUR_TEMPLATE_SYS_ID_HERE');
t.query();
if (t.next()) {
    gs.print(t.template);
}

Ran that and it didnt print anything.

 

The template example is below, this is just a test one i setup to show you the two template fields im trying to populate

 

find_real_file.png