- 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 04:19 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2022 05:32 AM
Hi,
why not hold those values in separate variable and then use them to set instead of array
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 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();