- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2023 05:57 AM
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));
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2023 08:02 AM
Hi @frviuf ,
Hope you are doing great.
To display Multi-Row Variable Set (MRVS) rows in a table and then generating a PDF:
Retrieve the MRVS data from the record
Using HTML and ServiceNow GlideForm API, you can construct a table dynamically to display the MRVS rows
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));
Regards,
Riya Verma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2023 08:02 AM
Hi @frviuf ,
Hope you are doing great.
To display Multi-Row Variable Set (MRVS) rows in a table and then generating a PDF:
Retrieve the MRVS data from the record
Using HTML and ServiceNow GlideForm API, you can construct a table dynamically to display the MRVS rows
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));
Regards,
Riya Verma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2023 01:57 AM
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');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2024 02:07 AM