Guest User

Untitled

a guest
May 28th, 2019
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * Query for the Finance task's related variables and print the varibale name and value on the Description field
  3. * Different handling done for the following variable types because value is different from the display value: Reference, Date, Select box
  4. * Also queries the multi-row variable set and prints them out below normal variables.
  5. */
  6.  
  7. (function executeRule(current, previous /*null when async*/) {
  8.     gs.info("BR started","multirow");
  9.     var session = gs.getSession();
  10.     var userLanguage = session.getLanguage().toString();
  11.     //var userLanguage = gs.getUser().getLanguage().toString();
  12.     var desc = '';
  13.     var queryVar = new GlideRecord('question_answer');
  14.     //Table Sys Id is current sys_id, Value is not empty, Variable type is not 'Label', Variable type is not 'HTML', Variable is not 'hasAttachment' and it's not a container
  15.     queryVar.addEncodedQuery('table_sys_id=' + current.sys_id + '^valueISNOTEMPTY^question.type!=11^question.type!=23^question!=4e91f02137b5ab005ef2005a54990e5d^question.type!=19^question.type!=20');
  16.     queryVar.orderBy('order');
  17.     queryVar.query();
  18.     while (queryVar.next()) {
  19.         gs.info("in first while","multirow");
  20.         var varQuestion = queryVar.getDisplayValue('question');
  21.         var variableSetType = queryVar.question.variable_set.type.toString(); //Get the variable set type
  22.         var varQuestionId = queryVar.getValue('question').toString();
  23.         var varValue = queryVar.getValue('value').toString();
  24.         var varReference = queryVar.question.reference.toString();
  25.         var varType = queryVar.question.type.toString();
  26.         //Only print values from single row variable sets and variables
  27.         if (variableSetType == 'one_to_one'){
  28.             //If variable type is 'Reference', fetch the display value from the referenced record
  29.             if (varType == '8') {
  30.                 //gs.info("in IF reference","multirow");
  31.                 var queryRef = new GlideRecord(varReference);
  32.                 queryRef.get(varValue);
  33.  
  34.                 //Check if Reference belongs to multi row variable set and don't print label:
  35.                 if (!varValue.startsWith('[{')){
  36.                     //Print Reference DisplayValue
  37.                     desc += varQuestion + ': ' + queryRef.getDisplayValue() + '\n';
  38.                 }
  39.  
  40.             }
  41.  
  42.             //If variable type is 'Date', parse the value into date, reformat it and turn to string
  43.             else if (varType == '9') {
  44.                 gs.info("in IF date","multirow");
  45.                 var date = new GlideDate();
  46.                 date.setValue(varValue);
  47.                 desc += varQuestion + ': ' + date.getByFormat('dd.MM.yyyy').toString() + '\n';
  48.             }
  49.             //If variable type is 'Select box' or 'Multiple Choice', fetch the display value of the choice Value
  50.             else if (varType == '5' || varType == '3') {
  51.                 gs.info("in IF multiple choice","multirow");
  52.                 var queryQuestChoice = new GlideRecord('question_choice');
  53.                 queryQuestChoice.addQuery('question', varQuestionId);
  54.                 queryQuestChoice.addQuery('value', varValue);
  55.                 queryQuestChoice.query();
  56.                 if (queryQuestChoice.next()) {
  57.  
  58.                     //If user is Finnish get the choice translations
  59.                     if (userLanguage == 'fi') {
  60.                         gs.info("in get translation","multirow");
  61.                         var queryTranslation = new GlideRecord('sys_translated');
  62.                         queryTranslation.addQuery('value', queryQuestChoice.text);
  63.                         queryTranslation.addQuery('language', 'fi');
  64.                         queryTranslation.addQuery('name', 'question_choice');
  65.                         queryTranslation.query();
  66.                         if (queryTranslation.next()) {
  67.                             desc += varQuestion + ': ' + queryTranslation.label + '\n';
  68.                         } else {
  69.                             desc += varQuestion + ': ' + queryQuestChoice.text + '\n';
  70.                         }
  71.                     } else {
  72.                         desc += varQuestion + ': ' + queryQuestChoice.text + '\n';
  73.  
  74.                     }
  75.                 }          
  76.             }
  77.  
  78.             // If variable type is 'CheckBox', only print if value is true
  79.             else if (varType == '7') {
  80.                 gs.info("in IF checkbox","multirow");
  81.                 if(varValue == 'true'){
  82.                     desc += varQuestion + ': ' + varValue + '\n';
  83.                 }
  84.                 //Otherwise just get the value
  85.             } else {
  86.                 desc += varQuestion + ': ' + varValue + '\n';
  87.             }
  88.         }
  89.     }
  90.    
  91.     //Multiline Variables values to Description
  92.     var multirow = new GlideRecord('sc_multi_row_question_answer');
  93.     multirow.addQuery('parent_id', current.sys_id);
  94.     multirow.orderBy("row_index");
  95.     multirow.orderBy("item_option_new.order");
  96.     multirow.query();
  97.     //template.print('<table style="width: 100%;">');
  98.    
  99.     for(var ii=0; ii < multirow.getRowCount(); ii++ ){
  100.         //gs.info("ii = " + ii);
  101.         var countL = countLabels(multirow);
  102.         var question_count = "";
  103.         multirow.next();
  104.        
  105.         if(ii == 0){
  106.             //template.print('<tr>');
  107.             desc += '\n';
  108.             question_count = printLabels(multirow);
  109.             question_count--;
  110.             //desc += '\n';
  111.         }
  112.        
  113.         //Print the values
  114.         printValue(multirow, ii, multirow.getRowCount());
  115.         var jaannos = ii % countL;
  116.        
  117.         //Works the manual way
  118.         /*
  119.         if(ii == 2){ //ii -> 2 using 3 labels with 4 labels use 3
  120.             gs.info("ii = 2 = "+ jaannos,"TSC LOG");
  121.             desc += '\n';
  122.         }
  123.        
  124.         }      
  125.         if(ii == multirow.getRowCount())
  126.             desc += '\n';
  127.         */
  128.     }
  129.    
  130.     current.description = desc;
  131.    
  132.     //functions for printing multirow variables labels and values
  133.     function printLabels(multirow){
  134.         var question_count = 0;
  135.         var varset = new GlideRecord("item_option_new");
  136.         varset.addQuery("variable_set",multirow.variable_set);
  137.         varset.orderBy("order");
  138.         varset.query();
  139.         while(varset.next()){
  140.             desc += varset.question_text.toString()+':'+'\n';
  141.            
  142.             //template.print('<td style="width: 15%; font-weight: bold;">'+varset.question_text.toString()+'</td>');
  143.             question_count++;
  144.         }
  145.         return question_count;
  146.     }  
  147.     function printValue(multirow, ii, rowCount){
  148.         //gs.info("in printvalue multirow","multirow");
  149.         var printable_value="";
  150.         if(multirow.question_answer.question.type == 6){//if type = text
  151.             printable_value = multirow.value.toString().trim();
  152.             //gs.info('String value: '+printable_value);
  153.         }
  154.         else if(multirow.question_answer.question.type == 8){ //if type reference
  155.             //gs.info('Type is: '+multirow.question_answer.question.type.toString());
  156.             var gr = new GlideRecord(multirow.question_answer.question.reference.toString());
  157.             gr.addQuery('sys_id', multirow.value.toString());
  158.             gr.query();
  159.             if (gr.next()){
  160.                 printable_value = gr.name.toString().trim();               
  161.             }
  162.         }
  163.         else if(multirow.question_answer.question.type == 5){//selectbox
  164.             var options = new GlideRecord('question_choice');
  165.             //options.addQuery('question', multirow.toString()); //Not needed
  166.             options.addQuery('value', multirow.value.toString());
  167.             options.query();
  168.             if(options.next()){
  169.                 printable_value = options.text.trim();
  170.             }
  171.         }
  172.         else{
  173.             printable_value = multirow.value.toString().trim();
  174.         }
  175.         desc += printable_value.toString().trim()+'\n';
  176.         //template.print('<td style="width: 15%;">'+printable_value+'</td>');
  177.     }
  178.    
  179.     function countLabels(multirow){
  180.         var questionCount = 0;
  181.         var varset = new GlideRecord("item_option_new");
  182.         varset.addQuery("variable_set",multirow.variable_set);
  183.         varset.orderBy("order");
  184.         varset.query();
  185.         while(varset.next()){
  186.             questionCount++;
  187.         }
  188.         return questionCount;
  189.     }
  190.    
  191. })(current, previous);
Advertisement
Add Comment
Please, Sign In to add comment