- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2019 10:37 AM
Hello,
I have a requirement from management to copy variables from a SCTASK to the worknotes fields and post them.
The Business rule i have has created a work note and posted it, BUT it has no information in it. Its submitting a blank worknote. Any idea on any mistakes in the script?
I have a Before Update BR with the condition When state changes to closed complete.
Script:
copy_variables();
function copy_variables() {
var gr = new GlideRecord('sc_req_item');
gr.addQuery('sys_id','=',current.sys_id);
gr.query();
var vtp = '';
var v = new GlideRecord('sc_item_option_mtom');
v.addQuery('request_item', current.sys_id);
v.orderBy('sc_item_option.order');
v.query();
while(v.next()) {
if (vtp.length >= 0) {
/*****************************
This code dynamically pulls the questions from the forms in the order they are presented (numerically)
and then displays them in a consistant readable format.
Right now nothing links to any records but can with some slight changes to this code.
Give the variable you want to fill in the u_for field on the RITM the name 'u_for'.
If you want the u_for to be either one of two fields name the second field 'u_for2'.
I have a script at the end that checks which of the two has a value, whichever does, fill that in the u_for field.
*****************************/
if (v.sc_item_option.value.getDisplayValue() == ''){/*if the value is blank, don't print*/}else
{
if (v.sc_item_option.item_option_new.type == 1){//Yes / No
vtp += v.sc_item_option.item_option_new.getDisplayValue() + ': ' + v.sc_item_option.value.getDisplayValue() + '\n';
}
if (v.sc_item_option.item_option_new.type == 2){//Multi Line Text
vtp += v.sc_item_option.item_option_new.getDisplayValue() + ': ' + v.sc_item_option.value.getDisplayValue() + '\n';
}
if (v.sc_item_option.item_option_new.type == 3){//Multiple Choice
vtp += v.sc_item_option.item_option_new.getDisplayValue() + ': ' + v.sc_item_option.value.getDisplayValue() + '\n';
}
if (v.sc_item_option.item_option_new.type == 4){//Numeric Scale
vtp += v.sc_item_option.item_option_new.getDisplayValue() + ': ' + v.sc_item_option.value.getDisplayValue() + '\n';
}
if (v.sc_item_option.item_option_new.type == 5){//Select Box
vtp += v.sc_item_option.item_option_new.getDisplayValue() + ': ' + v.sc_item_option.value.getDisplayValue() + '\n';
}
if (v.sc_item_option.item_option_new.type == 6){//Single Line Text
vtp += v.sc_item_option.item_option_new.getDisplayValue() + ': ' + v.sc_item_option.value.getDisplayValue() + '\n';
}
if (v.sc_item_option.item_option_new.type == 7){//Check Box
vtp += v.sc_item_option.item_option_new.getDisplayValue() + ': ' + v.sc_item_option.value.getDisplayValue() + '\n';
}
if (v.sc_item_option.item_option_new.type == 8){//Reference
var referencegr = new GlideRecord(v.sc_item_option.item_option_new.reference);
referencegr.addQuery('sys_id','=',v.sc_item_option.value);
referencegr.query();
while (referencegr.next()){
vtp += v.sc_item_option.item_option_new.getDisplayValue() + ': ' + referencegr.getDisplayValue() + '\n';
}
}
if (v.sc_item_option.item_option_new.type == 9){//Date
vtp += v.sc_item_option.item_option_new.getDisplayValue() + ': ' + v.sc_item_option.value.getDisplayValue() + '\n';
}
if (v.sc_item_option.item_option_new.type == 10){//Date/Time
vtp += v.sc_item_option.item_option_new.getDisplayValue() + ': ' + v.sc_item_option.value.getDisplayValue() + '\n';
}
if (v.sc_item_option.item_option_new.type == 11){//Label
//Do nothing
}
if (v.sc_item_option.item_option_new.type == 12){//Break
//Do nothing
}
if (v.sc_item_option.item_option_new.type == 13){//Not Listed
//Do nothing
}
if (v.sc_item_option.item_option_new.type == 14){//Macro
//Do nothing
}
if (v.sc_item_option.item_option_new.type == 15){//UI Page
//Do nothing
}
if (v.sc_item_option.item_option_new.type == 16){//Wide Single Line Text
vtp += v.sc_item_option.item_option_new.getDisplayValue() + ': ' + v.sc_item_option.value.getDisplayValue() + '\n';
}
if (v.sc_item_option.item_option_new.type == 17){//Macro with Label
//Do nothing
}
if (v.sc_item_option.item_option_new.type == 18){//Lookup Select Box
var lsbgr = new GlideRecord(v.sc_item_option.item_option_new.lookup_table);
lsbgr.addQuery('sys_id','=',v.sc_item_option.value);
lsbgr.query();
while (lsbgr.next()){
vtp += v.sc_item_option.item_option_new.getDisplayValue() + ': ' + lsbgr.getDisplayValue() + '\n';
}
}
if (v.sc_item_option.item_option_new.type == 19){//Container Start
//Do nothing
}
if (v.sc_item_option.item_option_new.type == 20){//Container End
//Do nothing
}
if (v.sc_item_option.item_option_new.type == 21){//List Collector
var list = v.sc_item_option.value.getDisplayValue();
var listarray = list.split(',');
vtp += v.sc_item_option.item_option_new.getDisplayValue() + '\n';
for (var i=0; i<listarray.length;){
igr = new GlideRecord(v.sc_item_option.item_option_new.list_table);
igr.addQuery('sys_id','=',listarray[i]);
igr.query();
while (igr.next()){
vtp += '- ' + igr.getDisplayValue() + '\n';//displayvalues
}
//vtp += 'i = ' + i + '- ' + listarray<i> + '\n';//sys_ids
i++;
}
}
if (v.sc_item_option.item_option_new.type == 22){//Lookup Multiple Choice //success
vtp += v.sc_item_option.item_option_new.getDisplayValue() + ': ' + v.sc_item_option.value.getDisplayValue() + '\n';
}
}
}
}
var wn = '\n';
wn+= vtp;
current.work_notes = wn;
}
Solved! Go to Solution.
- Labels:
-
Request Management
-
Service Catalog

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2019 08:21 AM
Try this I tested in my PDI as an after insert business rule on the sc_task table.
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(current.getValue('request_item'));
var workNote = "";
set.load();
var vs = set.getFlatQuestions();
gs.log ('Size: ' + vs.size());
for (var i=0; i < vs.size(); i++) {
if(vs.get(i).getDisplayValue() != '') { // Only get variables that were filled out
workNote = workNote + vs.get(i).getLabel() + ": " + vs.get(i).getDisplayValue() + "\n";
}
}
current.work_notes = workNote;
current.update();
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2019 11:01 AM
Check out the replies on this post:
https://community.servicenow.com/community?id=community_question&sys_id=e89e0f21dbdcdbc01dcaf3231f961957&view_source=searchResult
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2019 04:58 AM
Hello,
I have seen this article. I do not thing this code is up to date. Its giving me alot of errors.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2019 06:23 AM
Here would be my simple code to put variable info on sc_task worknotes
Note: Please mark reply as correct if it has answered your question OR mark it has helpful if it has provided the assistance to some extent
//Before Insert Business Rule on sc_task
for ( var v in current.variables ) {
if ( !JSUtil.nil(current.variables[v]) ) {
current.work_notes += "\n" + current.variables[v].getGlideObject().getQuestion().label + " : " + current.variables[v].getDisplayValue();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2019 06:56 AM
Hello,
This has gotten me further then any of my other actions. The only problem is it is only pulling the first variable and none of the others?
Thank You