Generate PDF file for catalog variables and attach it to RITM

L veerasivaredd
Kilo Explorer

Hello There,

I need to create a PDF file with catalog variables and attach it to RITM as a PDF file. Has anyone attempted this before? or do you have any suggestions? Could you please paste the script below if you have done this?

8 REPLIES 8

piyushdeveloper
Tera Contributor

Working code:

 

(function executeRule(current, previous /*null when async*/ ) {

var v = new sn_pdfgeneratorutils.PDFGenerationAPI();

var tableName = current.getTableName();
var columLabels = ['number']; //As number field is derived from extended table

var grDict = new GlideRecord('sys_dictionary');
grDict.addEncodedQuery('name=' + tableName + '^internal_type!=collection^internal_typeISNOTEMPTY');
grDict.query();

while (grDict.next()) {
columLabels.push(grDict.element.getDisplayValue() + '');
}

var fields = '';
fields += "<table style='font-family: arial, sans-serif;border-collapse: collapse;width: 100%;'>";
for (f in columLabels) {
if (!columLabels[f].startsWith('sys_')) { //to avoid sys fields
if (fields.length > 0 && current[columLabels[f]].toString() != "") //if value exists in fields


fields += "<tr><th style='border: 1px solid black;text-align: left;padding: 8px;'>" + current[columLabels[f]].getLabel() + "</th><th style='border: 1px solid black;text-align: left;padding: 8px;'>" + current[columLabels[f]].getDisplayValue() + "</th></tr>";
}

}

var getVariable = new GlideRecord("sc_item_option_mtom");
getVariable.addEncodedQuery("request_item=" + current.sys_id);
getVariable.query();
while (getVariable.next()) {
fields += "<tr><th style='border: 1px solid black;text-align: left;padding: 8px;'>" + getVariable.sc_item_option.item_option_new.getDisplayValue() + "</th><th style='border: 1px solid black;text-align: left;padding: 8px;'>" + getVariable.sc_item_option.value.getDisplayValue() + "</th></tr>";
}

fields += "</table>";
var result = v.convertToPDF(fields, tableName, current.getUniqueValue(), current.number);

//action.setRedirect(current);

})(current, previous);

thanks

@tanuja_g 

I replied to your question. This is an old thread and you might not get any response here

 

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

piyushdeveloper
Tera Contributor

(function executeRule(current, previous /*null when async*/ ) {

var v = new sn_pdfgeneratorutils.PDFGenerationAPI();

var tableName = current.getTableName();
var columLabels = ['number']; //As number field is derived from extended table

var grDict = new GlideRecord('sys_dictionary');
grDict.addEncodedQuery('name=' + tableName + '^internal_type!=collection^internal_typeISNOTEMPTY');
grDict.query();

while (grDict.next()) {
columLabels.push(grDict.element.getDisplayValue() + '');
}

var fields = '';
fields += "<table style='font-family: arial, sans-serif;border-collapse: collapse;width: 100%;'>";
for (f in columLabels) {
if (!columLabels[f].startsWith('sys_')) { //to avoid sys fields
if (fields.length > 0 && current[columLabels[f]].toString() != "") //if value exists in fields


fields += "<tr><th style='border: 1px solid black;text-align: left;padding: 8px;'>" + current[columLabels[f]].getLabel() + "</th><th style='border: 1px solid black;text-align: left;padding: 8px;'>" + current[columLabels[f]].getDisplayValue() + "</th></tr>";
}

}

var getVariable = new GlideRecord("sc_item_option_mtom");
getVariable.addEncodedQuery("request_item=" + current.sys_id);
getVariable.query();
while (getVariable.next()) {
fields += "<tr><th style='border: 1px solid black;text-align: left;padding: 8px;'>" + getVariable.sc_item_option.item_option_new.getDisplayValue() + "</th><th style='border: 1px solid black;text-align: left;padding: 8px;'>" + getVariable.sc_item_option.value.getDisplayValue() + "</th></tr>";
}

fields += "</table>";
var result = v.convertToPDF(fields, tableName, current.getUniqueValue(), current.number);

//action.setRedirect(current);

})(current, previous);