- 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-26-2022 09:46 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-26-2022 11:12 PM
Hi Vandna,
Thank you for sharing your thoughts but the requirement is somewhat similar to business rule where we select table and under "Action" section we provide template and based upon our input it creates/updates the record in selected table accordingly
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2022 12:03 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2022 12:55 AM
Hi,
please share some screenshots etc
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader