- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2018 08:25 AM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2018 01:12 PM
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.
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2018 11:22 AM
Did you add the sys_id of your template to the script before you ran it?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2018 11:24 AM
yes of course

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2018 12:19 PM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2018 12:26 PM
short_description=TEST^u_template_applied_by=^u_template_used=^EQ

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2018 01:12 PM
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.
}