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

Here you go:

find_real_file.png

 

So Table and Set to are custom field. 

If I select table as problem and selected the field as mentioned then upon updating the incident Problem record should be created with category as software and assignment group as capacity mgmt

@Ankur Bawiskar screen shot shared in previous comment.

The requirement is similar to business rule where we select table and under action section we select field to be set on selected table

Hi,

similar to what change template uses.

Did you check how OOB it works?

Regards
Ankur

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

@Ankur Bawiskar screen shot shared in previous comment.

The requirement is similar to business rule where we select table and under action section we select field to be set on selected table

Hi,

template value field holds the encoded query like this

description=Work Package created based on the Work Package template^short_description=Work Package

So what you can do is get the individual field values by using string manipulation and then use it

find_real_file.png

Regards
Ankur

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