how to mass update templates?

bbarnard
Kilo Contributor

Is there an easy way to mass update templates (sys_template). I would like to change a field in approx 700 templates and would prefer not to have to do this by hand. Looking at the value template in the GlideRecord, the template values appear to be stored as single text field making it possible to update via script but I'm hoping there is an alternate way to do this.

1 ACCEPTED SOLUTION

tltoulson
Kilo Sage

The scripted approach will probably be the best approach, especially if the field name and value are the same across all of your templates.   You can use a very simple script recipe to make it happen:



var gr = new GlideRecord('sys_template');


//gr.addEncodedQuery('your query here');


gr.query();


while (gr.next()) {


      gr.FIELD_NAME = (gr.FIELD_NAME + '').replace('FIELDNAME=FIELDVALUE', 'FIELDNAME=NEWVALUE');


      gr.update();


}



Just replace the Field Name, Field Value, and enable the addEncodedQuery line if needed (use the list view filter to obtain the encoded query).


View solution in original post

3 REPLIES 3

solutioningnow
Giga Guru

Hi Bryan,



Template values are stored as an encoded query, you can do mass update using script. Please refer below script for your reference:



var grTem = new GlideRecord('sys_template');


grTem.query();


while(grTem.next())


{


grTem.template += '^active=true';


grTem.update();


}



Please mark answer as correct/helpful, if it was really helpful.



Regards,


Solutioner


Logo.png


Enhance Knowledge NOW@ www.solutioningnow.com


http://www.solutioningnow.com/


tltoulson
Kilo Sage

The scripted approach will probably be the best approach, especially if the field name and value are the same across all of your templates.   You can use a very simple script recipe to make it happen:



var gr = new GlideRecord('sys_template');


//gr.addEncodedQuery('your query here');


gr.query();


while (gr.next()) {


      gr.FIELD_NAME = (gr.FIELD_NAME + '').replace('FIELDNAME=FIELDVALUE', 'FIELDNAME=NEWVALUE');


      gr.update();


}



Just replace the Field Name, Field Value, and enable the addEncodedQuery line if needed (use the list view filter to obtain the encoded query).


Can you explain this a little bit more? I am trying to update some reference fields on the template and its not working. No error, just nothing happens. The template points to reference fields on the incident table