how to get true display value form sc_item_option?

josh_tessaro
Giga Expert

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?

1 ACCEPTED SOLUTION

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'


};


View solution in original post

8 REPLIES 8

Hi Josh,



I think you should be able to access the variables in Mani's suggestion in the following way:



var name = 'NAME'; // variable name in each iteration


var value = item.variables[name].getDisplayValue();



I think your only other option would be to GlideRecord query the choice list table and reference tables for the display value. I think this option is probably better for performance.


https://oslovanetworks.com/variables-values-to-description/


Try to check this link as well.


It prints variables to Description field of RITM.


It works for Reference and Choice type of variabels as well.


You can   examine the code to find how it is working.


This is pretty cool and I could see myself using it for its intended purpose. Looking at the code snippet getting the variable details it is using Glide methods to get the values its needs.



I specifically need to be able to check custom attributes on variables which I will use to define display behaviors for those variables else-ware at various times. For example I added a column to item_option_new called 'u_hide_always' which I want to be able to access in my script include to determine whether to return it or not. I do not know how to access these custom columns using Glide methods.



I used the code form this to create the below to test with.



// ritm is a glide record object.


function printVars(ritm){


  for(variableName in gr.variables) {


      var go = gr.variables[variableName].getGlideObject();



      /* Question */


      var question = go.getQuestion();


      /* Label */


      var label = go.getQuestion().getLabel();


      /* Type (numeric value) */


      var type = new Number(go.getType());


      /* Order (numeric value) */


      var order = new Number(go.getQuestion().getOrder());



      gs.print('variableName ' + variableName);


      gs.print('label ' + label);


      gs.print('gr.variables[variableName] ' +gr.variables[variableName].getDisplayValue());


      gs.print("TEST_1 " + go.getBooleanAttribute('mandatory'));


      gs.print("TEST_2 " + go.mandatory);


      gs.print('TEST_3 ' + go.hasAttribute('mandatory'));


      gs.print('order ' + order);


      gs.print('type ' + type + '\n');


  }


}



Any idea what table the go GlideObject is from? I would like to be able to access variable attributes such as mandatory but it doe snot seem to work form here.


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'


};