Applying Template on Projects in Custom/Scoped Application
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-22-2015 11:38 PM
Hi,
We are extending the project table and project tasks tables. We want to apply template on these extended tables. However, When we try to apply template through scripts, We are getting an error "com.glide.script.fencing.MethodNotAllowedException: Function applyTemplate is not allowed in scope". It works fine when we manually go and apply templates. Can anyone help on this ?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2016 11:34 PM
I guess we have to request for ServiceNow to add the function to the available list of scoped API functions.
This could be done via an enhancement request in HI.
I would also like to see this function available for use.
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-11-2016 05:04 PM
I found a workaround.
Although applyTemplate(), is not allowed in scope, you can use the following code:
var sys_id_of_template = '56a8e507db6b26405accd5f0cf96190b';
var grObj = new GlideRecord('incident');
var t = new GlideTemplate.get(sys_id_of_template);
t.apply(grObj);
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-26-2017 09:25 PM
Thanks, but I tried this in Helsinki and it doesn't seem to work, I am getting another illegal method call error. Maybe they locked this workaround down now too.
If anyone is interested I rolled my own "applyTemplate" function. You could probably use this as a boilerplate, it works for my needs.
var applyTemplate = function(template,record){
if(template.getTableName() !== 'sys_template'){ return false;}
var fields = template.template.replace('^EQ','').split('^');
for(var i in fields){
var kv = fields[i].split('=');
var k = kv[0];
var v = kv[1];
if(record.hasOwnProperty(k)){
try {
record[k] = v;
} catch (e) {
return false;
}
}
}
};

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2017 06:32 PM
The code works in Istanbul, I just tested
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022