Generate PDF and attach to the record
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.
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.
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('' + pdfTable + '.do?PDF&sys_id=' + pdfSysID + '&sysparm_view=' + pdfView);
r.setAuthenticationProfile('basic', authProfileSysID);
r.saveResponseBodyAsAttachment(attachToTable, attachToSysID, fileName);
var response = r.execute();
//response.getStatusCode();
}
https://www.servicenow.com/community/developer-articles/generate-pdf-and-attach-to-the-record/ta-p/2330194