Generate PDF through UI Action and email notification?

JayD1
Kilo Guru

Dear Community,

I have a requirement where I want to generate pdf through UI Action and once we click on the UI Action it will trigger one notification where the pdf will be send as an attachment.

for that I have create below things:

UI Action 

----

Name - Generate PDF

Table - L1(custom table)

script:

 

 

gs.eventQueue('x_infte_intellectu.ia_certificate', current, '', '');
gs.addInfoMessage('Certificate sent');
action.setReturnURL(current);
var ia = new GlideRecord('x_infte_intellectu_ia');
ia.addQuery('sys_id', current.select_intellectual_asset);
ia.query();
if (ia.next()) {
    if (ia.status == '17') {
        ia.status = '30';
        ia.update();
    }

}

 

 

 
Notification is triggering through the event queue and certificate is currently created using email layout and email template.
 
As of now the certificate is generated and being shown in the notification body but it requires to send as an attachment.
I have created below script :
 

 

 

 

var htmlText = "create pdf";
var targetTable = "x_infte_intellectu_iav";
gs.addInfoMessage("target table"+targetTable);
var targettableSysid = "90a2e1c31bf825100e1ba935624bcb37";
var pdfName = "pdfgenerated";
var v = new sn_pdfgeneratorutils.PDFGenerationAPI;
gs.addInfoMessage("result"+ v);
var result = v.convertToPDF(htmlText, targetTable, targettableSysid, pdfName);
gs.addInfoMessage("result"+ result);

gs.info("PDF result :" + JSON.stringify(result));
var attachment = new GlideRecord("sys_attachment");
attachment.get(result.attachment_id); // replce sys_id of attchemnet your attachment id
var attcontent = new GlideSysAttachment().getContentStream(attachment.sys_id);
new GlideSysAttachment().writeContentStream(current, attachment.getValue("file_name"), attachment.getValue('content_type'), attcontent);

 

 

 

I am using the script in UI Action but it is not getting attached to the email notification.
 

 

Getting below infomessage 
Message
PDF result :{"message":" Given target record [x_infte_intellectu_iav - 90a2e1c31bf825100e1ba935624bcb37] does not exist.","request_id":"68235559c3e4711029f2728dc0013145","status":"failure"}
​

please guide me how do I accomplish the requirement.

 

 
Thanks in advance.
Edit table data inline
Please 
4 REPLIES 4

Sivaprathapredd
Tera Contributor

Hello,

 

In which table attachment is getting stored and for which table notification configured?

Please check "Include Attachment" is checked or not in Notification.

 

Thanks!!

JayD1
Kilo Guru

Hi Siva,

The attahment we have to generate from the notification - currently a Certificate like formate we are generating using email layout and template but it is getting displayed in the body when notification is sent.

Notification is being created on the same table where I have created the UI Action (custom table ).

I believe include attachment checkbox is something which will send all the attachments from triggering record as an attachment to email.

Thanks.

Ankur Bawiskar
Tera Patron
Tera Patron

@JayD1 

if you add file to the record on which notification is present and you check the checkbox of Include attachment then file will automatically go in the email

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

JayD1
Kilo Guru

Hi Ankur, Thanks for your response , that's actually not a file which we want to send rather it's a specific format of certificate created using email layout and email template where we are populating some data from other tables and created a certificate.That we are send as an attachment in an email.

however the Include attachment is checked on Noti level.