Generating PDF and attach to ticket after submit the catalog item.

prasannakumard
Tera Guru

Hi Team,

My requirement is, after submitting the catalog item, need to generate PDF with catalog item variables, and attach the PDF to ticket.

For that I wrote the following script in Workflow run script activity.

By using following code, PDF is generated and attached to ticket. But, when I open that PDF it is showing following error:

 

Failed to load PDF document.

 

Please see the below code: I can able to get the finaloutput as catalog variables.

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

 

Please let me know if any modifications in above code and open the PDF properly.

 

Thanks & Regards,

Prasanna Kumar

1 ACCEPTED SOLUTION

Thank you Ankur for your valuable suggestions.

 

As per my requirement, we have used the following Packaged app from Share:

https://community.servicenow.com/community?id=community_question&sys_id=126887eddb1cdbc01dcaf3231f96199d

 

Thanks & Regards,

Prasanna Kumar

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Prasanna,

So is the pdf file created? If yes is the content visible inside that.

Regards

Ankur

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

Hi Ankur,

The PDF is attached to ticket but it is showing above mentioned issue when I'm trying to open the attached PDF.

Please let me know possible solution for the same.

Thanks & Regards,

Prasanna Kumar

Hi Prasanna,

I don't think you can directly create a pdf file like this.

following links you can check:

https://community.servicenow.com/community?id=community_question&sys_id=e130cb61db98dbc01dcaf3231f961935

https://community.servicenow.com/community?id=community_question&sys_id=0ec387a5dbd8dbc01dcaf3231f9619f4

https://community.servicenow.com/community?id=community_question&sys_id=d94bcfeddb5cdbc01dcaf3231f9619a3

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Thank you Ankur for your valuable suggestions.

 

As per my requirement, we have used the following Packaged app from Share:

https://community.servicenow.com/community?id=community_question&sys_id=126887eddb1cdbc01dcaf3231f96199d

 

Thanks & Regards,

Prasanna Kumar