UI Page: Turn Multi Row Variable set on Custom Table to HTML Table

Dazler
Mega Sage

Hi,

 

I am having the hardest time, trying to get a Multi-Row Variable Set from a Custom Table in to UI Page as an HTML Table.  I have queried the record and used "variables.name-of-variable-set" and when I set a log I can see the JSON, but when I try and pass it to a foreach loop in the HTML, I get nothing.  I even parsed the JSON before I added to the HTML and still nothing.  

What the need is, we have a custom application that has a record producer form request.  After ticket is submitted, sometimes the reps have to update the variables on the ticket and if they do they would need to print the form, just the variables, not the whole page.  The best approach is UI action to UI Page and pass the values directly to the UI Page form and automatically trigger the print after about 1000ms this gives a little time for the values to populate on the form.  I can pass just regular field values, but I can't get the MRVS to work.

 

Has anyone done this before?  Any help would be appreciated.

 

1 ACCEPTED SOLUTION

Robert H
Mega Sage

Hello @Dazler ,

 

Here is a working solution that I've verified on my end:

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

    <!-- SERVER-SIDE JS: GET ARRAY OF OBJECTS -->
    <g:evaluate var="list" object="true">
        var gr = new GlideRecordSecure('sc_multi_row_question_answer');
        gr.addQuery('parent_id', 'SYS_ID OF TASK GOES HERE');
        gr.addQuery('variable_set.internal_name', 'INTERNAL NAME OF MRVS GOES HERE');
        gr.query();

        var results = {};
        while (gr.next()) {
        	var index = gr.getValue('row_index');
        	if (!results[index]) {
        		results[index] = {};
        	}
        	results[index][gr.item_option_new.name] = gr.value.toString();
        }

        var list = Object.keys(results).sort().map(rowId => results[rowId]);
    </g:evaluate>

    <table style="border:1px solid black !important;border-collapse:collapse !important;" width="100%" align="center" cellspacing="0" cellpadding="0">
        <thead>
            <tr style="background: sandybrown;">
                <th>Question 1</th>
                <th>Question 2</th>
                <th>Question 3</th>
                <th>Question 4</th>
            </tr>
        </thead>
        <tbody>
            <j:forEach var="jvar_row" items="${list}">
                <tr>
                    <td>${jvar_row['mrv_question_1']}</td>
                    <td>${jvar_row['mrv_question_2']}</td>
                    <td>${jvar_row['mrv_question_3']}</td>
                    <td>${jvar_row['mrv_question_4']}</td>
                </tr>
            </j:forEach>
        </tbody>
    </table>
</j:jelly>

 

Regards,

Robert

View solution in original post

7 REPLIES 7

Edxavier Robert
Mega Sage

Hi, can you paste your code here? You mention that you are getting a json file, also some screenshoots will help understand better.

 

Are you trying to extract the values of the MRVS?

Hi @Edxavier Robert 

 

Basically I am trying to get a extra the values of the MRVS into an HTML Table format in a UI Page.  Here is my script:  

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

    <!-- SERVER-SIDE JS: GET ARRAY OF OBJECTS -->
    <g:evaluate var="list" object="true">
        var variableDataArray = {};
        var resultArray = [];

        var gr = new GlideRecordSecure('sc_multi_row_question_answer');
        gr.addEncodedQuery('variable_set=XXX^parent_id=XXX');
        gr.query();

        while (gr.next()) {
        var rowIndex = gr.getValue('row_index');

        // Get item_option_new's sys_id
        var itemOptionSysId = gr.getValue('item_option_new');

        // Lookup the name from sc_item_option
        var itemOptionGr = new GlideRecordSecure('item_option_new');
        var questionName = '';
        if (itemOptionGr.get(itemOptionSysId)) {
        questionName = itemOptionGr.getValue('name'); // like 'mrv_question_1'
        }
        var value = gr.getValue('value');
        if (!variableDataArray[rowIndex]) {
        variableDataArray[rowIndex] = {};
        }
        variableDataArray[rowIndex][questionName] = value;
        }
        // Convert object to array
        for (var idx in variableDataArray) {
        resultArray.push(variableDataArray[idx]);
       }
        resultArray;
    </g:evaluate>

    <table style="border:1px solid black !important;border-collapse:collapse !important;" width="100%" align="center" cellspacing="0" cellpadding="0">
        <thead>
            <tr style="background: sandybrown;">
                <th>Question 1</th>
                <th>Question 2</th>
                <th>Question 3</th>
                <th>Question 4</th>
            </tr>
        </thead>
        <tbody>
            <j:forEach items="${list}" var="row">
                <tr>
                    <td colspan="4">Raw List: ${list}</td>
                </tr>
                <tr>
                    <td colspan="4">Row Debug: ${row}</td>
                </tr>

                <tr>
                    <td>${row['mrv_question_1']}</td>
                    <td>${row['mrv_question_2']}</td>
                    <td>${row['mrv_question_3']}</td>
                    <td>${row['mrv_question_4']}</td>
                </tr>

            </j:forEach>
        </tbody>
    </table>
</j:jelly>

 

I am getting not values in return. 

Dazler_0-1745034416668.png

 

Thank you for any help.

 

Hi, 

 

Can you try this code?

 

<?xml version="1.0" encoding="utf-8" ?>

<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide">

  <g:evaluate var="rows" object="true">

    var resultArray = [];

    var variableDataMap = {};

    

    var gr = new GlideRecordSecure('sc_multi_row_question_answer');

    gr.addQuery('variable_set', 'PUT_YOUR_MRVS_SYS_ID_HERE'); // MRVS sys_id

    gr.addQuery('parent_id', 'PUT_RITM_OR_TASK_SYS_ID_HERE'); // e.g., RITM sys_id

    gr.query();

 

    while (gr.next()) {

      var rowIndex = gr.getValue('row_index');

      var itemOptionGr = new GlideRecord('item_option_new');

      if (!variableDataMap[rowIndex]) {

        variableDataMap[rowIndex] = {};

      }

 

      if (itemOptionGr.get(gr.getValue('item_option_new'))) {

        var questionName = itemOptionGr.getValue('name');

        variableDataMap[rowIndex][questionName] = gr.getValue('value');

      }

    }

 

    for (var idx in variableDataMap) {

      resultArray.push(variableDataMap[idx]);

    }

 

    resultArray;

  </g:evaluate>

 

  <table border="1" style="border-collapse:collapse;width:100%;">

    <thead style="background-color:#f0ad4e;">

      <tr>

        <th>Question 1</th>

        <th>Question 2</th>

        <th>Question 3</th>

        <th>Question 4</th>

      </tr>

    </thead>

    <tbody>

      <j:forEach items="${rows}" var="row">

        <tr>

          <td>${row['mrv_question_1']}</td>

          <td>${row['mrv_question_2']}</td>

          <td>${row['mrv_question_3']}</td>

          <td>${row['mrv_question_4']}</td>

        </tr>

      </j:forEach>

    </tbody>

  </table>

</j:jelly>

@Dazler 

this link should help please enhance

How to Display Multi Row Variable set (MRVS) data in a notification 

How to convert MRVS in tabular form and then pdf 

also check this

How do I access MRVS values in a UI page? 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader