The Now Platform® Washington DC release is live. Watch now!
on 03-31-2017 12:03 AM
I was asked to provide a simple solution for generating a PDF export of one record and attach it to another one. Either if it makes sense or not from a process perspective, I cannot tell, but what I can share is simple and ready-to-use solution.
The solution below is server side script that can be used as e.g.Business Rule or in Script include.
Requirements:
You need to create a basic authentication profile (Create a basic auth profile) and since this might differ between your instances - let's put it in a system property.
Version: Helsinki+
Feel free to use!
Cheers Vladimir
var basicAuthProfileSysID = gs.getProperty('com.customer.pdf.authprofile');
generateAndStoreAttachment('incident', '12345678900000000000', 'Default', current.sys_class_name, current.sys_id,'Incident_Export.pdf', basicAuthProfileSysID);
/**
* Gets a PDF from a record form view and stores it as an attachment to the same record.
* Uses RestMessageV2 for getting the attachment via HTTP Call.
*
* All parameters are mandatory.
*
* @param {String} pdfTable Table of the record to be generated.
* @param {SysID} pdfSysID Sys ID of the record.
* @param {String} view View that should be used for generation of PDF
* @param {SysID} attachToTable Class name/table name to which the record should be attached.
* @param {SysID} attachToSysID Sys ID of the record that generated file should be attached to
* @param {String} fileName Target file name.
* @param {SysID} authProfileSysID SYSID of the profile to be used for authentication.
* @return null
*/
function generateAndStoreAttachment( pdfTable, pdfSysID, pdfView, attachToTable, attachToSysID, fileName, authProfileSysID ) {
var r = new sn_ws.RESTMessageV2();
r.setHttpMethod('get');
r.setEndpoint('https://'+gs.getProperty('instance_name')+'.service-now.com/' + pdfTable + '.do?PDF&sys_id=' + pdfSysID + '&sysparm_view=' + pdfView);
r.setAuthenticationProfile('basic', authProfileSysID);
r.saveResponseBodyAsAttachment(attachToTable, attachToSysID, fileName);
var response = r.execute();
//response.getStatusCode();
}
Thanks, it helped me
Thanks,
Surya Amara
PS: Hit like, Helpful or Correct depending on the impact of the response
Hi
I am trying to achieve this for a UI page.
On click of a button in UI page, the whole content of the page needs to be attached to the record from which UI page is opened.
In the processing script I am trying:
var attCreator = new GlideRecord('ecc_queue');
attCreator.agent = "AttachmentCreator";
attCreator.topic = "AttachmentCreator";
attCreator.name = 'NewFile' + ":" + 'application/pdf';
attCreator.source = 'my_table'+":"+'<sys_id> of the record';
var encodedData = gs.base64Encode(payL); // the content need to be put in pdf
attCreator.payload = gs.base64Encode(payL);
attCreator.insert();
After this, I got the attachment attached to the record but the content is not there in the pdf, it says "Failed to load PDF document.".
Can anyone please help on this
Thanks
Deepak
Hi @Vladimir Kobetic
I have tried above solution but it is not working for me.
I want current records PDF file should get attached to current record.
Please let me know how to achieve it.
Regards,
Sajan
Hi Sajan
Please have a look:
Love PDF? PDF loves you too . . . PDF Generator | Service Now Mantra
Mark correct if it helps.
Thanks
Deepak
Thanks Very helpful !!
Hi Sajan,
Did you implement this functionality? May i know the steps to implement the above functionality??
Works great! Awesome work!
Hello Vladimir,
This a great solution and Im trying to implement it in our instance. I believe however Im getting hung up on the basic auth profile. Ive created one, however not entirely sure what to do with the REST message for the endpoint. Any help would be much much appreciated, thanks
Steve
Instead of:
r.setAuthenticationProfile(‘basic’, authProfileSysID);
I had to replace it with:
r.setBasicAuth(‘<user_name)’, ‘<password>’);
And I created that user with the Admin role
Hi, do i have to create this Rest Message? What is sn_ws?
And we dont use Passwords, we are using ADFS Connection.
The setAuthenticationProfile seems to be legacy, does somebody has a Solution?