The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Apply template server side

Lasse5
Kilo Contributor

Hello!

We have a setup from where we generate an array of tasks to a project depending on a customer input value. The relation between the projects and tasks happens through a related list.

When the user has selected the value on the project, and submitted it, the list automatically creates the tasks through a BR / sys_properties.

Each of these respective tasks which are generated through the project, should be populated depending on a template they are linked to. For example: task 1.0 is mapped to template 1.0. We have done this through a client script:

var task = g_form.getValue('task_no');

var task1_0 = 'task_id';

//var task 1_1='task_id'

etc.

if(task == "1.0"){
applyTemplate(task1_0);

This solution does work. However, the template is first applied, once the user goes into the task (due to being a client script), and is thus not loaded directly in the related list. The data first becomes visible through the related list, when the user has entered the respective tasks and saved them.

My question is then, it it possible via a client script to have the data loaded directly? Or alternatively, can I apply templates via business rules, to be able to load the data server-side?

I have been testing the various solutions posted here: https://docs.servicenow.com/bundle/london-platform-administration/page/administer/form-administration/reference/r_ScriptedTemplates.html and nothing works.

Thanks in advance!

5 REPLIES 5

deepak105
Kilo Expert

Taking an example if we want to create an incident from script (obviously server side scripting requires Access to GlideRecord), we do the applyTemplate as 

var inc = new GlideRecord('incident');

inc.applyTemplate('template_name');

 

Now you can write a script include which will be called through glideAjax from client script. The script include will create task applying template which can be passed to as parameter. 

Or if you use business rule then you can directly create task from there(I mean inserting into whatever table it relates to). 

Lasse5
Kilo Contributor

Thanks for your quick reply!

So how does this work?

I would have to create a client script along these lines:

function onLoad() {

var location=new GlideAjax('Get_task_template);

task.addParam('sysparm_name','GetTask');

task.addParam('task_ID',task_name);

task.getXMLWait();

str_location=location.getAnswer();

if(task=='task 1.1')       // template will be applied for user with location Paris

{

      applyTemplate('155224634f23320073f9cab18110c77f');

}

Then for the script include we would have to write something along: 

var Get_task_template = Class.create();


Get_task_template.prototype = Object.extendsObject(AbstractAjaxProcessor, {


GetTask:function(){


var task=this.getParameter('task_ID');


var task='task_no';


var rec=new GlideRecord('task_table');


rec.addQuery('task',task_ID);


rec.query();


rec.next();


taskqu=rec.taskqu;


return taskqu;


},
      type: 'Get_task_template'
});

you have mixed location and task varaible. please check that from your side.

task.addParam('task_ID',task_name);

whatever custom parameter we pass it should start like sysparm_yourparametername.

so for you in client script it should be 

location.addParam('sysparm_task_ID',task_name);

 

and in the script include  the below line should be replaced as

var task=this.getParameter('task_ID');

var task = this.getParameter('sysparm_task_ID');

then do query accordingly.

Lasse5
Kilo Contributor

Yes, sorry. I found something fast to get a reference point to what we needed to fulfill the different scripts. We just made it work; however, the same issue is yet persisting.

The related list still does not show the data as soon as the task has been created. Thus, the data is still first being loaded when the user enters the form, which leaves the related list looking empty.