Copy SCTask Variables to work notes

aj_becker
Tera Guru

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;
}

1 ACCEPTED SOLUTION

Brian Lancaster
Tera Sage

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);

View solution in original post

6 REPLIES 6

Hi,

 

I corrected the code, you can use below one.

 

Note: Please mark reply as correct if it has answered your question

 

(function executeRule(current, previous /*null when async*/) {

	var variableInfo = '';
	
	for ( var v in current.request_item.variables ) {
		if ( !JSUtil.nil(current.request_item.variables[v]) ) {
			variableInfo = variableInfo + "\n" + current.request_item.variables[v].getGlideObject().getQuestion().label + " : " + current.request_item.variables[v].getDisplayValue();
			
		}
	}
	if (variableInfo)
		current.work_notes = variableInfo;


})(current, previous);

Brian Lancaster
Tera Sage

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);