How to convert MRVS in tabular form and then pdf

frviuf
Tera Contributor

I want to display MRVS(multi row variable set) rows in a table and then in a pdf.

 

This script attaches the pdf attachment to the record, but I don't know how to access the MRVS variables from the record.

 

Any advice or guidance is appreciated.

This would be useful for requests.

var fieldMap = new Object();
fieldMap["date"] = "date";
fieldMap["name"] = "name";
fieldMap["email"] = "email";

var flatten = new Object();
flatten["FlattenType"] = "partially_flatten";

var v = new sn_pdfgeneratorutils.PDFGenerationAPI;
var result = v.fillDocumentFieldsAndFlatten(fieldMap, "<attachmentSysId>", "<tableName>", "<tableSysId>", "pdfName", flatten);
gs.info(JSON.stringify(result));

 

1 ACCEPTED SOLUTION

Riya Verma
Kilo Sage
Kilo Sage

Hi @frviuf ,

 

Hope you are doing great.

To display Multi-Row Variable Set (MRVS) rows in a table and then generating a PDF:

  1. Retrieve the MRVS data from the record

  2. Using HTML and ServiceNow GlideForm API, you can construct a table dynamically to display the MRVS rows

  3. Generate a PDF with the table content: Utilizing the sn_pdfgeneratorutils.PDFGenerationAPI, you can generate a PDF document that includes the constructed table

code for reference:

 

// Step 1: Retrieve MRVS data
var mrvsGr = new GlideRecord('<MRVS_Table_Name>');
mrvsGr.addQuery('parent', '<tableSysId>'); // Assuming parent field connects MRVS to the main record
mrvsGr.query();

// Step 2: Build the table
var tableHtml = '<table><thead><tr><th>Date</th><th>Name</th><th>Email</th></tr></thead><tbody>';
while (mrvsGr.next()) {
    tableHtml += '<tr><td>' + mrvsGr.getValue('date') + '</td><td>' + mrvsGr.getValue('name') + '</td><td>' + mrvsGr.getValue('email') + '</td></tr>';
}
tableHtml += '</tbody></table>';

// Step 3: Generate the PDF
var fieldMap = new Object();
fieldMap["tableContent"] = tableHtml;

var flatten = new Object();
flatten["FlattenType"] = "partially_flatten";

var pdfApi = new sn_pdfgeneratorutils.PDFGenerationAPI();
var result = pdfApi.fillDocumentFieldsAndFlatten(fieldMap, '<attachmentSysId>', '<tableName>', '<tableSysId>', 'pdfName', flatten);
gs.info(JSON.stringify(result));

 

 

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

View solution in original post

3 REPLIES 3

Riya Verma
Kilo Sage
Kilo Sage

Hi @frviuf ,

 

Hope you are doing great.

To display Multi-Row Variable Set (MRVS) rows in a table and then generating a PDF:

  1. Retrieve the MRVS data from the record

  2. Using HTML and ServiceNow GlideForm API, you can construct a table dynamically to display the MRVS rows

  3. Generate a PDF with the table content: Utilizing the sn_pdfgeneratorutils.PDFGenerationAPI, you can generate a PDF document that includes the constructed table

code for reference:

 

// Step 1: Retrieve MRVS data
var mrvsGr = new GlideRecord('<MRVS_Table_Name>');
mrvsGr.addQuery('parent', '<tableSysId>'); // Assuming parent field connects MRVS to the main record
mrvsGr.query();

// Step 2: Build the table
var tableHtml = '<table><thead><tr><th>Date</th><th>Name</th><th>Email</th></tr></thead><tbody>';
while (mrvsGr.next()) {
    tableHtml += '<tr><td>' + mrvsGr.getValue('date') + '</td><td>' + mrvsGr.getValue('name') + '</td><td>' + mrvsGr.getValue('email') + '</td></tr>';
}
tableHtml += '</tbody></table>';

// Step 3: Generate the PDF
var fieldMap = new Object();
fieldMap["tableContent"] = tableHtml;

var flatten = new Object();
flatten["FlattenType"] = "partially_flatten";

var pdfApi = new sn_pdfgeneratorutils.PDFGenerationAPI();
var result = pdfApi.fillDocumentFieldsAndFlatten(fieldMap, '<attachmentSysId>', '<tableName>', '<tableSysId>', 'pdfName', flatten);
gs.info(JSON.stringify(result));

 

 

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

Hi Riya,

 

Thank you for your reply. This was super helpful to point me in the right direction. 

I modified your suggestion to fit my requirements.

Thanks again.

// Step 1: Retrieve MRVS data
var idnum = '<recordSys_id>';

var gr = new GlideRecord('<tableName>');
if(gr.get(idnum)){
var variables = gr.variables.getElements();
var rowcount = gr.variables.<variablesetName>.getRowCount();

// Step 2: Build the table
var tableHtml = '<table><thead><tr><th>Date</th><th>Name</th><th>Email</th></tr></thead><tbody>';
for(var rc=0;rc<rowcount;rc++){
var row = gr.variables.tablecontent.getRow(rc);
tableHtml  += '<tr><td>' + row.date + '</td><td>' + row.name + '</td><td>' + row.email + '</td></tr>';
}
tableHtml  += '</tbody></table>';

// Step 3: Generate the PDF
var flatten = new Object();
flatten["FlattenType"] = "partially_flatten";

new sn_pdfgeneratorutils.PDFGenerationAPI().convertToPDF(tableHtml , '<tableName>', '<recordSys_id>','pdfName');
}

 

Iain5
Tera Contributor

Often using the community, rarely have anything to share.  This question helped me deliver on something recently.  Here's a Script Include I created to support this in a re-usable way.