We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

How to display table records in an HTML variable in a catalog item from the onload catalog script

peterscaramuzzo
Tera Expert

I want to display the contents of a table filtering on a specific field in an HTML variable on a catalog item. I put the code I found in an onload script but can't seem to get it to work. Is there an out of the box server script include I can use? I assume TableDataLoader is an example of a custom server side script that needs to be created?

 

function onLoad() {
    var ga = new GlideAjax('TableDataLoader');
    ga.addParam('sysparm_name', 'getMatchingRecords');
    ga.addParam('sysparm_table_name', 'myTable');
    ga.addParam('sysparm_encoded_query', 'tableColumn=xxxx');

    ga.getXML(processResponse);
    g_form.setValue('myHTMLvariable', response);
}

function processResponse(response) {
    alert('function start');
    var answer = response.responseXML.documentElement.getAttribute("answer");
    var records = JSON.parse(answer);

    var tableHeaders = ['Column1, 'Column2']; // Headers for your table
    var tableHTML = '<table border="2" style="width: 100%; border-collapse: collapse;"><thead><tr>';

    // Add headers
    for (var i = 0; i < tableHeaders.length; i++) {
        tableHTML += '<th style="padding: 5px;">' + tableHeaders[i] + '</th>';
    }
    tableHTML += '</tr></thead><tbody>';

    // Add rows
    if (records.length > 0) {
        for (var j = 0; j < records.length; j++) {
         tableHTML += '<tr>';
         tableHTML += '<td style="padding: 5px;">' + records[j].u_column1 + '</td>';
         tableHTML += '<td style="padding: 5px;">' + records[j].u_column2 + '</td>';
         tableHTML += '</tr>';
        }
        document.getElementById('no_records').style.display = 'none';
    } else {
        document.getElementById('no_records').style.display = '';
    }

    tableHTML += '</tbody></table>';
    document.getElementById('dvTable').innerHTML = tableHTML;
}
1 REPLY 1

Sandeep Rajput
Tera Patron

@peterscaramuzzo If your catalog item variable is of type HTML then you simply need to use g_form.setValue('<name of the HTML variable>',tableHTML); to render the HTML in HTML variable. No need to use

document.getElementById('dvTable').innerHTML = tableHTML;