Issue in Pdf report upload to incident as an attachment
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-12-2024 02:08 PM
Hi Team,
I am trying to upload a PDF to an existing incident and it seems it has an encoding issue, so i cannot see/open pdf from an incident.
Test code i am using is as this: Please help me.
// Define the Incident record Sys ID to which the attachment will be added
var recordSysId = 'xxx'; // Replace with the actual Incident Sys ID later
var tableName = 'incident'; // The table name to which the file will be attached
// Define the URL of the PDF file to fetch
var pdfUrl = 'https://pdfobject.com/pdf/sample.pdf';
try {
// Step 1: Fetch the PDF file from the URL using RESTMessageV2
var restMessage = new sn_ws.RESTMessageV2(); // Create RESTMessageV2 object
restMessage.setHttpMethod('GET');
restMessage.setEndpoint(pdfUrl);
// Execute the request
var response = restMessage.execute();
var statusCode = response.getStatusCode();
if (statusCode == 200) {
gs.addInfoMessage('getRequestHeaders : ' + JSON.stringify(response.getHeaders())) ;
// Successfully fetched the PDF
var responseBody = response.getBody(); // Get the PDF content (Base64 encoded) if save this pdf is blank
var contentType = response.getHeader('Content-Type'); // Get the content type
var fileName = 'report.pdf'; // Set the filename
// Step 2: Upload the PDF as an Incident attachment
var attachment = new GlideSysAttachment();
var binaryData = GlideStringUtil.base64DecodeAsBytes(responseBody); // Decode Base64 content to binary when save this corrupt pdf i.e. pdf cannot load
//set up inputs
var rec = new GlideRecord('incident');
rec.get(recordSysId);
aa = attachment.write(rec, fileName, contentType, binaryData);
gs.addInfoMessage(aa);
// Optional: Add a log or info message to confirm attachment creation
gs.addInfoMessage('PDF file successfully attached to the Incident record.');
} else {
// Handle the case where the PDF could not be fetched
gs.addErrorMessage('Failed to fetch PDF file. HTTP Status Code: ' + statusCode);
}
} catch (ex) {
gs.info(contentType);
gs.info(responseBody);
gs.logError('Error occurred while fetching and attaching PDF: ' + ex.getMessage());
}