- 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 01:46 PM
Thanks. Got my script working, this URL helped
https://community.servicenow.com/community?id=community_question&sys_id=118213a1db101fc01dcaf3231f961936

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2018 01:52 PM
Nice. You'll notice I've got that in the script above as well. Please mark that answer as correct if I've answered your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2018 01:55 PM
Yes it does look right, it was the gr.next that was causing the problem. Thanks for the responses!