How would I go about creating a script to update some fields added in an incident template?

TrenaFritsche
Tera Expert

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

1 ACCEPTED SOLUTION

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!

View solution in original post

5 REPLIES 5

Allen Andreas
Administrator
Administrator

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!

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?

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!

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');

}