- 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 08:37 AM
Please check below post which has solution for your requirement.
https://community.servicenow.com/community?id=community_question&sys_id=7eb7c7e9db1cdbc01dcaf3231f96197f
Regads,
Sachin

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2018 08:41 AM
Can you paste your script here so I can have a look?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-07-2018 08:58 AM
The general idea is that your script will need to run from the 'Scripts -> Background' module for all of the templates you want to modify. The script should look like this...but will need to be modified to only target your specific templates you want to update (using the commented 'addQuery' line) and to add only those values you want to add (using the 'encodedString' variable). If you set up a template like you want (with the new values), you can set up a quick business rule to log out 'current.template' so you can see what the encodedString needs to look like.
var encodedString = '^active=true';
var grTem = new GlideRecord('sys_template');
//grTem.addQuery(); // Add more query lines to restrict just to templates that need updating
grTem.query();
gs.print(grTem.getRowCount()); // Print out the number of records being updated
while (grTem.next()) {
grTem.template += encodedString;
gr.setWorkflow(false); // Don't run business rules, etc.
gr.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 10:03 AM
Tried this and various other variations also, nothing works
var gr = new GlideRecord('sys_template');
gr.setWorkflow(false);
gr.addQuery('table', '=', 'Incident');
gr.addQuery('name', '=', 'TEMPLATENAME');
gr.query();
while (gr.next())
{
gr.template = (gr.template +'' ).replace('u_template_used=', 'u_template_used=TEMPLATENAME);
gr.update();
}