Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Query for the Finance task's related variables and print the varibale name and value on the Description field
- * Different handling done for the following variable types because value is different from the display value: Reference, Date, Select box
- * Also queries the multi-row variable set and prints them out below normal variables.
- */
- (function executeRule(current, previous /*null when async*/) {
- gs.info("BR started","multirow");
- var session = gs.getSession();
- var userLanguage = session.getLanguage().toString();
- //var userLanguage = gs.getUser().getLanguage().toString();
- var desc = '';
- var queryVar = new GlideRecord('question_answer');
- //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
- queryVar.addEncodedQuery('table_sys_id=' + current.sys_id + '^valueISNOTEMPTY^question.type!=11^question.type!=23^question!=4e91f02137b5ab005ef2005a54990e5d^question.type!=19^question.type!=20');
- queryVar.orderBy('order');
- queryVar.query();
- while (queryVar.next()) {
- gs.info("in first while","multirow");
- var varQuestion = queryVar.getDisplayValue('question');
- var variableSetType = queryVar.question.variable_set.type.toString(); //Get the variable set type
- var varQuestionId = queryVar.getValue('question').toString();
- var varValue = queryVar.getValue('value').toString();
- var varReference = queryVar.question.reference.toString();
- var varType = queryVar.question.type.toString();
- //Only print values from single row variable sets and variables
- if (variableSetType == 'one_to_one'){
- //If variable type is 'Reference', fetch the display value from the referenced record
- if (varType == '8') {
- //gs.info("in IF reference","multirow");
- var queryRef = new GlideRecord(varReference);
- queryRef.get(varValue);
- //Check if Reference belongs to multi row variable set and don't print label:
- if (!varValue.startsWith('[{')){
- //Print Reference DisplayValue
- desc += varQuestion + ': ' + queryRef.getDisplayValue() + '\n';
- }
- }
- //If variable type is 'Date', parse the value into date, reformat it and turn to string
- else if (varType == '9') {
- gs.info("in IF date","multirow");
- var date = new GlideDate();
- date.setValue(varValue);
- desc += varQuestion + ': ' + date.getByFormat('dd.MM.yyyy').toString() + '\n';
- }
- //If variable type is 'Select box' or 'Multiple Choice', fetch the display value of the choice Value
- else if (varType == '5' || varType == '3') {
- gs.info("in IF multiple choice","multirow");
- var queryQuestChoice = new GlideRecord('question_choice');
- queryQuestChoice.addQuery('question', varQuestionId);
- queryQuestChoice.addQuery('value', varValue);
- queryQuestChoice.query();
- if (queryQuestChoice.next()) {
- //If user is Finnish get the choice translations
- if (userLanguage == 'fi') {
- gs.info("in get translation","multirow");
- var queryTranslation = new GlideRecord('sys_translated');
- queryTranslation.addQuery('value', queryQuestChoice.text);
- queryTranslation.addQuery('language', 'fi');
- queryTranslation.addQuery('name', 'question_choice');
- queryTranslation.query();
- if (queryTranslation.next()) {
- desc += varQuestion + ': ' + queryTranslation.label + '\n';
- } else {
- desc += varQuestion + ': ' + queryQuestChoice.text + '\n';
- }
- } else {
- desc += varQuestion + ': ' + queryQuestChoice.text + '\n';
- }
- }
- }
- // If variable type is 'CheckBox', only print if value is true
- else if (varType == '7') {
- gs.info("in IF checkbox","multirow");
- if(varValue == 'true'){
- desc += varQuestion + ': ' + varValue + '\n';
- }
- //Otherwise just get the value
- } else {
- desc += varQuestion + ': ' + varValue + '\n';
- }
- }
- }
- //Multiline Variables values to Description
- var multirow = new GlideRecord('sc_multi_row_question_answer');
- multirow.addQuery('parent_id', current.sys_id);
- multirow.orderBy("row_index");
- multirow.orderBy("item_option_new.order");
- multirow.query();
- //template.print('<table style="width: 100%;">');
- for(var ii=0; ii < multirow.getRowCount(); ii++ ){
- //gs.info("ii = " + ii);
- var countL = countLabels(multirow);
- var question_count = "";
- multirow.next();
- if(ii == 0){
- //template.print('<tr>');
- desc += '\n';
- question_count = printLabels(multirow);
- question_count--;
- //desc += '\n';
- }
- //Print the values
- printValue(multirow, ii, multirow.getRowCount());
- var jaannos = ii % countL;
- //Works the manual way
- /*
- if(ii == 2){ //ii -> 2 using 3 labels with 4 labels use 3
- gs.info("ii = 2 = "+ jaannos,"TSC LOG");
- desc += '\n';
- }
- }
- if(ii == multirow.getRowCount())
- desc += '\n';
- */
- }
- current.description = desc;
- //functions for printing multirow variables labels and values
- function printLabels(multirow){
- var question_count = 0;
- var varset = new GlideRecord("item_option_new");
- varset.addQuery("variable_set",multirow.variable_set);
- varset.orderBy("order");
- varset.query();
- while(varset.next()){
- desc += varset.question_text.toString()+':'+'\n';
- //template.print('<td style="width: 15%; font-weight: bold;">'+varset.question_text.toString()+'</td>');
- question_count++;
- }
- return question_count;
- }
- function printValue(multirow, ii, rowCount){
- //gs.info("in printvalue multirow","multirow");
- var printable_value="";
- if(multirow.question_answer.question.type == 6){//if type = text
- printable_value = multirow.value.toString().trim();
- //gs.info('String value: '+printable_value);
- }
- else if(multirow.question_answer.question.type == 8){ //if type reference
- //gs.info('Type is: '+multirow.question_answer.question.type.toString());
- var gr = new GlideRecord(multirow.question_answer.question.reference.toString());
- gr.addQuery('sys_id', multirow.value.toString());
- gr.query();
- if (gr.next()){
- printable_value = gr.name.toString().trim();
- }
- }
- else if(multirow.question_answer.question.type == 5){//selectbox
- var options = new GlideRecord('question_choice');
- //options.addQuery('question', multirow.toString()); //Not needed
- options.addQuery('value', multirow.value.toString());
- options.query();
- if(options.next()){
- printable_value = options.text.trim();
- }
- }
- else{
- printable_value = multirow.value.toString().trim();
- }
- desc += printable_value.toString().trim()+'\n';
- //template.print('<td style="width: 15%;">'+printable_value+'</td>');
- }
- function countLabels(multirow){
- var questionCount = 0;
- var varset = new GlideRecord("item_option_new");
- varset.addQuery("variable_set",multirow.variable_set);
- varset.orderBy("order");
- varset.query();
- while(varset.next()){
- questionCount++;
- }
- return questionCount;
- }
- })(current, previous);
Advertisement
Add Comment
Please, Sign In to add comment