Set reference fields via template

martin_chabarri
Mega Expert

Hi all,

I am trying to set Model Category and Model on the alm_hardware table via template, but am not able to do so. It seems that dot walked fields, or maybe reference dot walked fields, cannot be applied via template via script. In my script include, I am using the applyTemplate method(), and I believe this is working because all other fields are being set properly. ACL's are not an issue because I can create a new record and use the same template and it works.

HELP!

var gr = new GlideRecord(table_name);

gr.initialize();

gr.applyTemplate(template_name);

gr.setTimeout("",2000); //is it a timing issue?

gr.insert();

1 ACCEPTED SOLUTION

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

It's undocumented, but I have seen this code in Event Management business rule where you can apply a template via SysId using the following syntax:


GlideTemplate.get(template.sys_id).apply(GlideRecord)



You might want to try this instead of using applyTemplate() and see if this works.


View solution in original post

11 REPLIES 11

Chuck Tomasi
Tera Patron

Can you include a screenshot of the dot-walked/reference fields in the template that are giving you the trouble?



My suspicion is that templates set values on a record for a specific table and update that record. If you are trying to set fields on other records in other tables, that's not going to work.


find_real_file.png


This is the template



find_real_file.png


I think I understand. Thanks Martin.



So here's what's happening behind the scenes... The template field is stored as an encoded query (same as a list filter) to set values. Example:



active=true^Name=New computer



When you invoke this via the UI by clicking the template name, it is parsing and placing the values on the various form fields. You save them when you hit Submit.



It is as I suspected... When you do this via script, you are actually trying to set a field (parent.model_category) on another table. The gr.insert(), is only updating the data for the "gr" record, not the parent. As far as I know, there's no single method to set fields on record A, and related fields on record B with one method (e.g. update the dot-walked fields.)