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

Thanks @piyushdeveloper, It is working fine. Could you please confirm, if there are other PDF generation API's available in SNOW, and where?

Sohail Khilji
Kilo Patron
Kilo Patron

This must help : WE have used this for one of the client, alter this code and it must work for generating pdf and attaching to a record.

 

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

    // Add your code here
    var htmlBody;
    var gr = new GlideRecord('dmn_demand');
    gr.get(current.sys_id);

    htmlBody = getParsedBody(gr, '4ed828ac93ffb1108f1e326efaba1089');

    var pdfApi = new sn_pdfgeneratorutils.PDFGenerationAPI;

    var hfInfo = new Object();
    hfInfo["PageSize"] = "LETTER";
    hfInfo["PageOrientation"] = "PORTRAIT";
    hfInfo["GeneratePageNumber"] = "false";
    hfInfo["TopOrBottomMargin"] = "36";
    hfInfo["LeftOrRightMargin"] = "14";

    var res = pdfApi.convertToPDFWithHeaderFooter(htmlBody, 'dmn_demand', gr.sys_id, 'Access Permission - ', hfInfo);

    var grAttachment = new GlideRecord('sys_attachment');
    if (grAttachment.get(res.attachment_id.toString())) {
        grAttachment.table_name = 'dmn_demand';
        grAttachment.table_sys_id = current.sys_id;
        grAttachment.update();

    }

    function getParsedBody(gr, templateID) {
        var parsedBody = '';
        var parsedBodyClosed = '';
        var template = new GlideRecord('sysevent_email_template');
        if (template.get(templateID)) {
            var docBody = template.getDisplayValue('message_html');
            parsedBody = docBody;
        }

        // parsing of variables dynamically
        var sampleString = docBody.toString();
        var reg = new SNC.Regex('/\\$\\{(.*?)\\}/i');
        var match = reg.match(sampleString);
        var count = 0;
        var variables = [];
        var values = [];
        var tmpValue;
        while (match != null) {
            variables.push(match.toString().substring(match.toString().indexOf(',') + 1));
            match = reg.match();

            values.push(variables[count]);
            if (gr.getDisplayValue(values[count]) == null || JSUtil.nil(gr.getDisplayValue(values[count]))) {
                tmpValue = '';
            } else {
                tmpValue = gr.getDisplayValue(values[count]);
                tmpValue = tmpValue.replace(/<p>[{!Page_Break}]<\/p>/g, '!Page_Break');

            }
            parsedBody = parsedBody.replace('${' + variables[count] + '}', tmpValue);
            count++;

        }
        return parsedBody;
    }

})(current, previous);

☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect