Set Field from template_value type field

mr18
Tera Guru
Tera Guru

I have created two custom field on incident table:
1. Table type field
2. Template_value type field

Requirement is: If I am selecting table as problem and define some template then upon insert/update of incident, record should be created in selected table and set the field based on template

For example: if I am selecting 'problem' as a table and select category as "X" under template value field then upon insert/update of incident new problem record should be created with category as X.

1 ACCEPTED SOLUTION

-O-
Kilo Patron
Kilo Patron

ServiceNow provides an example of how to do this using prototype GlideTemplate. One can find it in Script Include global.TestExecutorAjax and its function decodeFieldValues:

// Use GlideTemplate so we can re-use existing code.
var fakeTemplateGR = new GlideRecord("sys_template");
fakeTemplateGR.setValue("template", encodedQuery);
fakeTemplateGR.setValue("next_child",false);
fakeTemplateGR.setValue("table",table);
		
// GlideTemplate is a Java class used when applying templates from the UI
var glideTemplate = GlideTemplate.getFromRecord(fakeTemplateGR);
glideTemplate.setApplyChildren(false); // n/a for this use case
var targetGR = glideTemplate.apply();
// ...

You could modify it to suite your need, as below (assuming current is the incident in the Business Rule and assuming the names of the fields are u_table and u_set_to):

// Use GlideTemplate so we can re-use existing code.
var fakeTemplateGR = new GlideRecord("sys_template");

fakeTemplateGR.setValue("template", '' + current.u_set_to); // This will add the values selected by the user to the fake temporary template
fakeTemplateGR.setValue("next_child", false);
fakeTemplateGR.setValue("table", '' + current.u_table);

// GlideTemplate is a Java class used when applying templates from the UI
var glideTemplate = GlideTemplate.getFromRecord(fakeTemplateGR);

glideTemplate.setApplyChildren(false);

var newRecord = new GlideRecord('' + current.u_table);

// Begin a new record in the selected table
newRecord.newRecord();

// Apply the values selected to the new record
glideTemplate.apply(newRecord);

// Set other values that must take precedence over the template, like the parent - for instance (assuming table will always be a task):
newRecord.parent = current.getUniqueValue();

// Create the new record
newRecord.insert();

View solution in original post

12 REPLIES 12

@Ankur Bawiskar This is code I tried

var table = current.u_table; //custom field on incident
var templates = current.u_set_to; // custom template field on incident
var arr = templates.split('^');
for (var i = 0; i < arr.length; i++) {

var arrnew = arr[i].split('=');
for (var j = 0; j < arrnew.length-1; j = j + 2)
var field = arrnew[j]; // to capture field name

for (var t = 1; t < arrnew.length; t = t + 1)
var value = arrnew[t]; // to capture the value

var gr_prb = new GlideRecord(table);
gr_prb.initialize();
gr_prb.parent = gr.sys_id;
gr_prb.field = value; // this where the issue is
  }
gr_task.insert();

 

The code is working fine except the third last line: gr_prb.field = value

Due to which it is unable to set the selected values

Hi,

why not hold those values in separate variable and then use them to set instead of array

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

-O-
Kilo Patron
Kilo Patron

ServiceNow provides an example of how to do this using prototype GlideTemplate. One can find it in Script Include global.TestExecutorAjax and its function decodeFieldValues:

// Use GlideTemplate so we can re-use existing code.
var fakeTemplateGR = new GlideRecord("sys_template");
fakeTemplateGR.setValue("template", encodedQuery);
fakeTemplateGR.setValue("next_child",false);
fakeTemplateGR.setValue("table",table);
		
// GlideTemplate is a Java class used when applying templates from the UI
var glideTemplate = GlideTemplate.getFromRecord(fakeTemplateGR);
glideTemplate.setApplyChildren(false); // n/a for this use case
var targetGR = glideTemplate.apply();
// ...

You could modify it to suite your need, as below (assuming current is the incident in the Business Rule and assuming the names of the fields are u_table and u_set_to):

// Use GlideTemplate so we can re-use existing code.
var fakeTemplateGR = new GlideRecord("sys_template");

fakeTemplateGR.setValue("template", '' + current.u_set_to); // This will add the values selected by the user to the fake temporary template
fakeTemplateGR.setValue("next_child", false);
fakeTemplateGR.setValue("table", '' + current.u_table);

// GlideTemplate is a Java class used when applying templates from the UI
var glideTemplate = GlideTemplate.getFromRecord(fakeTemplateGR);

glideTemplate.setApplyChildren(false);

var newRecord = new GlideRecord('' + current.u_table);

// Begin a new record in the selected table
newRecord.newRecord();

// Apply the values selected to the new record
glideTemplate.apply(newRecord);

// Set other values that must take precedence over the template, like the parent - for instance (assuming table will always be a task):
newRecord.parent = current.getUniqueValue();

// Create the new record
newRecord.insert();