Applying Template on Projects in Custom/Scoped Application

shobhit_bhagat
Kilo Explorer

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 ?

9 REPLIES 9

The SN Nerd
Giga Sage
Giga Sage

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

The SN Nerd
Giga Sage
Giga Sage

shobhit.bhagat



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

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;


              }            


      }    


  }


};


The code works in Istanbul, I just tested



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022