- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-24-2015 11:13 AM
When doing a lookup against sc_item_option you can access the variable value however if it is a select box it holds teh value of the option not the display_value and if its a reference field it holds the sys_id not the display value for the referenced record.
Is there a clean way to go form the value of all variable types to the display value?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-25-2015 02:52 PM
I used part of what I was doing and a lot of what you linked and created this which fits my needs. Hopefully it is not too inefficient.
var CatalogUTILS = Class.create();
CatalogUTILS.prototype = {
initialize: function() {
/* map: element vs internal field type */
this.YES_NO = 1;
this.MULTI_LINE_TEXT = 2;
this.MULTIPLE_CHOICE = 3;
this.NUMERIC_SCALE = 4;
this.SELECT_BOX = 5;
this.SINGLE_LINE_TEXT = 6;
this.CHECKBOX = 7;
this.REFERENCE = 8;
this.DATE = 9;
this.DATE_TIME = 10;
this.LABEL = 11;
this.BREAK = 12;
this.MACRO = 14;
this.UI_PAGE = 15;
this.WIDE_SINGLE_LINE_TEXT = 16;
this.MACRO_WITH_LABEL = 17;
this.LOOKUP_SELECT_BOX = 18;
this.CONTAINER_START = 19;
this.CONTAINER_END = 20;
this.LIST_COLLECTOR = 21;
this.LOOKUP_MULTIPLE_CHOICE = 22;
this.HTML = 23;
this.SPLIT = 24;
this.MASKED = 25;
/* this is a list of elements that will be ignored */
this.IGNORE = [this.UI_PAGE, this.CONTAINER_START, this.CONTAINER_END, this.HTML, this.MACRO,
this.MACRO_WITH_LABEL, this.SPLIT, this.LABEL];
},
/*
* sysID sys_id of RITM
* isEML true|false to use email rulesy
* noBlanks true|false to avoid empty variable's value (optional - default:false)
*/
getVariables: function(sysID, isEML, showBlanks) {
if(typeof iSEML === 'undefined') { isEML = false; }
if(typeof noBlanks === 'undefined') { noBlanks = false; }
/* get records on the sc_item_option_mtom table for teh sysID of the teh passed sc_req_item */
var vars = new GlideRecord('sc_item_option_mtom');
vars.addQuery('request_item', sysID);
vars.query();
var array = [];
/* foreach variable on the passed sc_req_item */
while(vars.next()){
var dict = vars.sc_item_option.item_option_new;
var v = vars.request_item.variables[dict.name];
var o = {
'name': dict.name,
'question': dict.question_text,
'value': v,
'display_value': this._getDisplayVal(v, dict.type),
'type': new Number(dict.type),
'order': new Number(dict.order),
'is_wf_var': dict.sys_class_name != 'item_option_new',
'hide_always_eml': dict.u_hide_always_eml,
'hide_empty_eml': dict.u_hide_empty_eml
};
/* if the item does not need ot be skipped, add it to the array. */
if(!(isEML && o.hide_always_eml) && !( ((isEML && o.hide_empty_eml) || noBlanks) && gs.nil(o.value) ) && !this._isIgnored(o.type)){
array.push(o);
}
}
return this._quicksort(array);
},
/* quick sort algorithm */
_quicksort: function(arr) {
if (arr.length === 0) {
return [];
}
var left = [];
var right = [];
var pivot = arr[0];
for (var i = 1; i < arr.length; i++) {
/* compare order from object */
if(this._compare(pivot,arr[i])) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return this._quicksort(left).concat(pivot, this._quicksort(right));
},
/* custom compare function */
_compare: function(a, b){
if(!a.wf_var && b.wf_var)
return false;
if(a.wf_var && !b.wf_var)
return true;
return a.order > b.order;
},
/* check if the given field type should be ignored */
_isIgnored: function(type) {
for(i=0; i < this.IGNORE.length; i++)
if( this.IGNORE[i] == type)
return true;
return false;
},
_getDisplayVal: function(val,type){
if(type == this.REFERENCE || type == this.LIST_COLLECTOR || type == this.MULTIPLE_CHOICE || type == this.SELECT_BOX) {
return val.getDisplayValue();
}
return val;
},
type: 'CatalogUTILS'
};

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-24-2015 11:26 AM
Where are you accessing that value from? I don't think there's an easy, quick way to do it, but you do have some options.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-24-2015 11:30 AM
I am writing a script include to get various information about a RITM variable including some custom fields and return it in an array of all variables on the item. It works except for the issue with the value not always showing the display value.
var CatalogUTILS = Class.create();
CatalogUTILS.prototype = {
initialize: function() {},
getVariablesEML: function(sysID) {
function Variable(question,value,type,order,sys_class_name,hide_always_eml,hide_empty_eml) {
this.question = question;
this.value = value;
this.type = type;
this.order = order;
this.wf_var = sys_class_name != 'item_option_new';
this.hide_always_eml = hide_always_eml;
this.hide_empty_eml = hide_empty_eml;
}
function compareVars(a, b){
if(!a.wf_var && b.wf_var)
return -1;
if(a.wf_var && !b.wf_var)
return 1;
return a.order - b.order;
}
var temp = '';
var ar = [];
var gr = new GlideRecord('sc_item_option_mtom');
gr.addQuery('request_item', sysID);
gr.query();
//gs.log('CatalogUTILS.getVariables(' + sysID + ') returns ' + gr.getRowCount() + ' records');
while(gr.next()){
temp = new Variable(gr.sc_item_option.item_option_new.question_text,gr.sc_item_option.display_value,gr.sc_item_option.item_option_new.type,gr.sc_item_option.item_option_new.order,gr.sc_item_option.item_option_new.sys_class_name,gr.sc_item_option.item_option_new.u_hide_always_eml,gr.sc_item_option.item_option_new.u_hide_empty_eml);
if(!temp.hide_always_eml && !(temp.hide_empty_eml && temp.value == '') && temp.type != 11 && temp.type !=19 && temp.type !=20){
ar.push(temp);
}
}
gs.log('CatalogUTILS.getVariables(' + sysID + ') returns ' + ar.length + ' records');
return ar.sort(compareVars);
},
type: 'CatalogUTILS'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-24-2015 11:54 AM
Josh,
If you have the function 'getVariablesEML()' and sys_id is being sent is of RITM.
So, to read the variables, you can directly use:
var item = new GlideRecord('sc_req_item');
item.get(sysID);
gs.log(item.variables.ref_variable.getDisplayValue()) will give you the value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-24-2015 01:15 PM
This is true however when iterating through the questions I would need a way to dynamically set the ref_variable in your example, such as:
var name = 'NAME' // variable name in each iteration
var value = item.variables.' + name + '.getDisplayValue()
Additionally I am a bit concerned about performance when it comes to adding additional Glide queries. I still do not have a good grasp of how impact adding additional queries is.