Has Anyone created a dynamic ricktext label for a catalog item that uses a catalog client script?

Community Alums
Not applicable

I have a need to build a dynamic table that will show up on a Catalog item.

I have a table that is used to populate the table.  I was looking at some information out on the web and found that this could be done, but not having any luck getting it to produce the results.

 

I have a Rich Text Label named: efc_access_table that I want the script to populate.

I am using the code here for the Catalog Client Script:  But it's not populating anything.  The Catalog client script is set for onLoad for the catalog item and I'm not sure why it's not populating

 

function onLoad() {
    // You can replace 'your_table_name' with the actual table you want to query
    var gr = new GlideRecord('u_efc_choice');
	gr.query();

    // Create the HTML for the table dynamically
    var tableHtml = '<style>';
    tableHtml += 'table { width: 100%; border-collapse: collapse; }';
    tableHtml += 'th, td { padding: 8px; text-align: left; border: 1px solid #ddd; }';
    tableHtml += 'tr:nth-child(even) { background-color: #f2f2f2; }';
    tableHtml += 'tr:nth-child(odd) { background-color: #ffffff; }';
    tableHtml += '</style>';
    tableHtml += '<table>';
    tableHtml += '<thead><tr><th>Security Group</th><th>Description</th><th>Approver1</th><th>Approver2></th><th>Approver 3</th></tr></thead>';
    tableHtml += '<tbody>';

    // Loop through the records and add rows to the table
    var rowCount = 0;
    while (gr.next()) {
        tableHtml += '<tr>';
        tableHtml += '<td>' + gr.getValue('u_security_group') + '</td>';
        tableHtml += '<td>' + gr.getValue('u_description') + '</td>';
        tableHtml += '<td>' + gr.getValue('u_approver1.name') + '</td>';
		tableHtml += '<td>' + gr.getValue('u_approver2.name') + '</td>';
		tableHtml += '<td>' + gr.getValue('u_approver3.name') + '</td>';
        tableHtml += '</tr>';
        rowCount++;
    }

    tableHtml += '</tbody>';
    tableHtml += '</table>';

    // Inject the table HTML into the Rich Text field (e.g., with document.getElementById)
    g_form.setValue('efc_access_table', tableHtml);  
}

 
Has anyone created one of these that could shed some light on this please?

1 ACCEPTED SOLUTION

Community Alums
Not applicable

I was able to solve me own problem, I was using the g_form.setValue() function but it ended up I needed g_form.setLabelOf() function.

View solution in original post

6 REPLIES 6

jcmings
Mega Sage

Out of curiosity, why are you using a rich text label rather than a multi-row variable set or a custom widget?

Community Alums
Not applicable

It’s for a table on a catalog item that will just show the row data that I need.  It’s more of a helper table for the user because you can’t put the pop-up help on a list collector

Gotcha. If you need it to be editable, you can continue down the path of using a HTML-type variable (as Ankur mentioned below). If not, you can set that field read-only. Alternatively, you can use a custom widget. Either way, you'll use GlideAjax and a script include. If you need help getting started with GlideAjax, here's a post from a few weeks back that will help .

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

GlideRecord won't work on portal

Also your variable should be of type HTML and not Rich Text

So here is the workaround

1) use onLoad catalog client script + GlideAjax

2) that script include function will return the HTML and you can set it directly in that variable and make that variable read-only

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