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

Did you add the sys_id of your template to the script before you ran it?

yes of course

Okay, run this script (with your sys_id) and send me back what you get.

// Query for working template by sys_id
var temp = new GlideRecord('sys_template');
temp.get('YOUR_SYS_ID_HERE');
gs.print(temp.template);

short_description=TEST^u_template_applied_by=^u_template_used=^EQ

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.
}