Is there a way to add a field to an existing template and populate it via script?

Casey23
Tera Guru

Hello all. I have a background script created to update values on pretty much all of our templates in the system. However, we would like to add a field to all of the templates and populate the value of the field without having to go into each template manually. In the script below I'm trying to update the 'u_template' field with the value from the 'name' field (long story so don't ask haha) but since the u_template field isn't in any of the templates, it's not actually updating the record. Is there a way I can add the u_template field to the templates without doing it individually? TIA

// Create a new GlideRecord instance for the sys_template table
var templateGr = new GlideRecord('sys_template');
var count = 0;

// Add a condition to filter active templates where the global checkbox is false
templateGr.addQuery();
templateGr.addQuery('active', true);
templateGr.addQuery('global', true);

// Execute the query
templateGr.query();

// Loop through the results
while (templateGr._next()) {
    count++;
    //templateGr.global = true; // Was previously used to set the global vlue to true
    templateGr.u_template = templateGr.name; // Set the template name field to the name of the template
    templateGr.update(); // Update the record
    gs.info('Template name: ' + templateGr.name);
    gs.info('Template name: ' + templateGr.u_template);
}
gs.info(count);

 

1 ACCEPTED SOLUTION

Casey23
Tera Guru

According to support, this is not possible:

"Thank you for your update. I have checked this further internally and also with a team lead and unfortunately, there is no ServiceNow API that is able to achieve the functionality you are looking for.

We apologize for the inconvenience this may cause. As mentioned previously if you think this can be a useful functionality, then we recommend creating an enhancement request

We have moved this case to the solution proposed state. Please let us know if you have any additional questions. Otherwise, you may accept the solution and bring the case to a close. "

View solution in original post

9 REPLIES 9

There may be ACLs or dictionary overrides stopping you.  SN Support will be able to help you.  you can review the node log after testing to possibly get some insight.

This also isn't working in an OOB instance so I don't think this is something specific to our instance.

did you run the script logging in as 'admin' user? That is how I tested your logic. I got one failure due to Application Scope restrictions.

Casey Verhagen
Giga Guru

According to support, this is not possible:



"Thank you for your update. I have checked this further internally and also with a team lead and unfortunately, there is no ServiceNow API that is able to achieve the functionality you are looking for.

We apologize for the inconvenience this may cause. As mentioned previously if you think this can be a useful functionality, then we recommend creating an enhancement request

We have moved this case to the solution proposed state. Please let us know if you have any additional questions. Otherwise, you may accept the solution and bring the case to a close. "

Casey23
Tera Guru

According to support, this is not possible:

"Thank you for your update. I have checked this further internally and also with a team lead and unfortunately, there is no ServiceNow API that is able to achieve the functionality you are looking for.

We apologize for the inconvenience this may cause. As mentioned previously if you think this can be a useful functionality, then we recommend creating an enhancement request

We have moved this case to the solution proposed state. Please let us know if you have any additional questions. Otherwise, you may accept the solution and bring the case to a close. "