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

That unfortunately makes sense.



Any suggestions? Sounds like I would also not be able to do this by calling a script include in the template condition, being that it is still server side. How can I invoke a client side script and do this from the browser once the record is open?


I don't have any suggestions at this time.



As far as I know, there's no way to apply the template from a client script, however any onChange client script has an 'isTemplate' flag to determine if the field was changed on account of a template or a human manually changing it.


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.


Thanks I'll give it a try and let you know


Also just noticed your template is setting the "parent.model category" versus the "Model category" that exists on the alm_hardware table.   Is there a reason you are trying to set the parent?   Change it to model_category and see if that works.