Generating PDF of the prefilled catalog variables and attach to request(sc_req_item) after submit the catalog item and send an email to the user ABC.

Abhishek Kumar8
Tera Contributor

Hi, 

Generating PDF of the prefilled catalog variables and attach to request(sc_req_item) after submit the catalog item and send an email to the user ABC.

I came across a workflow script : 

var varquestion='';
var varanswer='';
var itemjoin="";
var itemquestion = "";
var itemanswer='';
var v;

/* Put all variable values and labels from the variable pool into an array */

var sortedArray = sortVariables(current.variables); gs.log("@@@insiderresult sortedArray: "+sortedArray);

for (var i in sortedArray) {
v = current.variables[sortedArray[i].index];
/* Only include non-empty variables, and exclude Label and Container variables */
if (v != '' && v != 'false' && v.getGlideObject().getQuestion().type != 11 && v.getGlideObject().getQuestion().type != 19 && v.getGlideObject().getQuestion().type != 20) {
itemquestion += v.getGlideObject().getQuestion().getLabel() + ":"+ v.getDisplayValue();
}
}

varquestion += itemquestion;
var finaloutput=varquestion; gs.log("@@@insiderresult finaloutput: "+finaloutput);

function sortVariables ( variableArray ){
var sortedVariables = [];
var count = 0;
for ( var i in variableArray ){
var object = {index:i, order:variableArray[i].getGlideObject().getQuestion().order};
sortedVariables[count] = object;
count++;
}
sortedVariables.sort( function compare(a,b){return a.order - b.order});
gs.log("@@@insiderresult Sortvariables: "+sortedVariables);
return sortedVariables;
}

//converts the file to xls format with file name as RITM number (considering workflow is on RITM table) & is attached to the RITM record
var attachfile = new GlideSysAttachment();

attachfile.write(current, current.number+'.pdf','application/pdf', finaloutput);

 Fault description: TypeError : line 31( var object = {index:i, order:variableArray[i].getGlideObject().getQuestion().order};) showing "can't convert null to an object"

any help on this is really appreciated.

Thank you,

11 REPLIES 11

Hi Abhishek,

Did you check current.variables gives variables under variable set or ont

Regards
Ankur

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

piyushdeveloper
Tera Contributor

Working code:

(function executeRule(current, previous ) {

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 gr = new GlideRecord('sc_req_item');
if (gr.get(current.sys_id)) {
var variables = gr.variables.getElements();
for (var i = 0; i < variables.length; i++) {
var question = variables[i].getQuestion();
fields += "<tr><th style='border: 1px solid black;text-align: left;padding: 8px;'>" + question.getLabel() + "</th><th style='border: 1px solid black;text-align: left;padding: 8px;'>" + question.getDisplayValue() + "</th></tr>";
}
}

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

//action.setRedirect(current);

})(current, previous);

Would this be added to the workflow, a BR, or how would you use this if you wanted it as part of a UI Action? 

You can use this code as a server side script in any place you want.

Hi @piyushdeveloper ,

 

Could you please confirm, where the PDF file will be stored after executing the code. Whether it will automatically be attached to the RITM or not?