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?

9 REPLIES 9

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 ,

 

Thanks for this script it works but the problem is for reference fields it is showing the sysid how to rectify that

I have modify the script as per my requirement 

(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("question_answer");
getVariable.addEncodedQuery("table_sys_id=" + current.sys_id);
getVariable.query();
while (getVariable.next()) {
fields += "<tr><th style='border: 1px solid black;text-align: left;padding: 8px;'>" + getVariable.question.getDisplayValue() + "</th><th style='border: 1px solid black;text-align: left;padding: 8px;'>" +getVariable.value.getDisplayValue() + "</th></tr>";
}

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

//action.setRedirect(current);

})(current, previous);

Can you please help me on this reference field part were it is showing the syid
the problem is in question answer table also im getting value as sysid only

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);