- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-26-2022 11:51 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2022 04:28 AM
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();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2022 01:32 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2022 02:16 AM
The requirement is similar to business rule where we select table and under action section we select field to be set on selected table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2022 02:26 AM
Hi,
similar to what change template uses.
Did you check how OOB it works?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2022 02:16 AM
The requirement is similar to business rule where we select table and under action section we select field to be set on selected table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2022 02:37 AM
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
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader