
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2020 11:43 AM
How would I go about creating a script to update some fields added in an incident template where the field is being changed to a different field. There are potentially thousands of templates and I would like to automate the search and find and replace various field names. ie: a template has a custom field that is getting populated with a value, but that custom field is no longer being used and a different field will need to replace it. If someone can just provide me tables to query that hold this template configuration, I should be able to figure out how to do it, but I am stuck when looking at a Template and see the field Template in sys_template is of type Template? I'm missing something here. Anyone know how to do this?
Thanks so much in advance!
Trena Fritsche
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2020 03:06 PM
Hi,
Please use _next like:
while (gr._next()) {
See my edited code above.
This is because the template table actually have a column called: next, so then your next use variation is _next.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2020 11:57 AM
Hi,
The table is sys_template.
A bit of the wonkiness comes from the templates are basically like a huge encodedquery.
Here's a snipped of what a setValue line would look line in a Client Script:
g_form.setValue('template', 'active=true^assignment_group=d625dccec0a8016700a222a0f7900d06^incident_state=2');
So you could basically attempt to query the sys_template table...for the template field, for that back-end name of the field and attempt to simply replace it that way, and save it back.
So something like:
var gr = new GlideRecord('sys_template');
gr.addQuery('table', 'incident');
gr.addQuery('template', 'CONTAINS', "old_field");
gr.query();
while (gr._next()) {
var string = gr.template.toString();
string.replace('old_field', 'new_field');
gr.setValue('template', string);
gr.update();
}
I'd recommend testing that with a specific sys_id record first...and do some log/print statements to see what you got before, what you're changing it to, what it ends up like before implementing wider.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2020 02:08 PM
I have been trying multiple ways to gs.info('In Loop'); inside the while loop when querying sys_template and I do not get this output. I get output before the while (or if for just one record) of a count, but its like it never goes inside the while loop to do some logic on the row. Any idea of what might be going on? I've tried this in a clients instance and also my pdi and get the same results.
Can you try a simple query like this and see if you get output?
var gr = new GlideRecord('sys_template');
gr.query();
var count = gr.getRowCount();
gs.info('count is' + count);
while (gr.next()) {
gs.info('In Loop');
}
gs.info('Done');
When I do this via background script or Fix Script, the only output in the log is:
count is 50
Done
Trying to understand why no logging inside the loop?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2020 03:06 PM
Hi,
Please use _next like:
while (gr._next()) {
See my edited code above.
This is because the template table actually have a column called: next, so then your next use variation is _next.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2020 04:28 PM
Thank you very much! I will keep that in mind if I come across this type thing again! That did work. I was going to consider this coding also...I'm guessing this will work also?
var gr = new GlideRecord('sys_template');
if(gr.get('sys_id','<sysid>)) {
gs.info('In Loop');
}