0

I am able to successfully attach PDF file with ServiceNow table record using GlideSysAttachment API and attachment.write() function in script, however whenever I download and try to open same, I get the error shown in below screenshot.

Code snippet

(function execute() {
    try{
          var rec = new GlideRecord('incident');
          var attachment = new GlideSysAttachment();
          var incidentSysID = incident.number;
          rec.get(incidentSysID);
          var fileName = 'Test_Incident.pdf';
          var contentType = 'application/pdf'; // Also tried with contentType as 'text/pdf'
          var content = pdf_content;
          var agr = attachment.write(rec, fileName, contentType, content);<br>
          gs.info('The PDF attachment sys_id is: ' + agr);
    }catch(err){
         gs.log('Got Error: ' + err);
         gs.info(err);
    }
})()

I also tried "AttachmentCreator" with ecc_queue within script but same error occurs. Below is code for it.

(function execute()
    {var attCreator = new GlideRecord('ecc_queue');
    attCreator.agent = "AttachmentCreator";
    attCreator.topic = "AttachmentCreator";
    attCreator.name = "Test.pdf" + ":" + "text/pdf"; 
    //Also tried, "Test.pdf:application/pdf"
    attCreator.source = "incident"+":"+ incident.number; 
    // Record Table name and sys_id of the particular record
    var content = pdf_content; // pdf_content is any string variable
    var stringUtil = new GlideStringUtil();
    var base64String = stringUtil.base64Encode(content);
    var isValid=GlideStringUtil.isBase64(base64String);  
    var base64String= gs.base64Encode(content);
    gs.info("Is valid base64 format in ecc_queue ? "+ isValid);
    attCreator.payload = base64String; //base64 content of the file
    attCreator.insert();
})()

I am able to attach and view excel and word files with similar scripts without any issues. I have checked system properties for attachments but everything looks fine. I am able to view the PDF file uploaded from UI to particular table records however not the one I attach via REST API or scripts.

I have also tried sending encoded data as bytes, base64 or simple string but nothing seems to work. I don't get any errors and attachment id is returned each time on creation of attachment.

Error Message - Opened PDF fails to load

After modifying my code slightly for above functions w.r.t scoped application instead of global; I got some information from logs when I debug:

  • 05:38:38.411 Security restricted: File type is not allowed or does not match the content for file Test.pdf
  • 05:38:38.410 Security restricted: MIME type mismatch for file: Test.pdf. Expected type:application/pdf, Actual type: text/plain
  • 05:38:38.394 App:XYZ App x_272539_xyz_ap: Is valid base64 format in ecc_queue ? true
Vanjuli
  • 59
  • 10

1 Answers1

0

First a comment: This line in your code is accidentally working -- make sure you understand that a task number is not the object sys_id

var incidentSysID = incident.number; // should be incident.sys_id

Next, it's unclear where the PDF content is coming from. IF your complete code is listed, I would expect the errors given as you have noted that pdf_content is "any string variable."

ServiceNow does have a the capability to create a PDF from an HTML argument. Generating a PDF from HTML

Here's a helpful blog post for getting a PDF (Platform generated) of an existing record: Love PDF? PDF loves you too

Valor
  • 345
  • 2
  • 7
  • Hi Valor, Thanks for your reply, however I am not looking for HTML or table form conversion to PDF but trying to create attachment using Service Now Attachment API in PDF format. Also, incident.number actually stores sys_id only. I referred GlideSysAttachment API for Server Scoped Applications at: https://developer.servicenow.com/dev.do#!/reference/api/newyork/server/no-namespace/c_GlideSysAttachmentScopedAPI#r_SGSA-write_GR_S_S_S. Further, pdf_content is variable containing some plain text data as string. – Vanjuli Dec 20 '20 at 13:18
  • 1
    Late reply, I know. You cannot pass plain text and have it magically get converted to a PDF file. Also, incident.number *does not store the sys_id.* The sys_id is a 32-character hexadecimal guid that typically never gets shown, but the awareness needs to be there by developers – Valor Jan 16 '22 at 21:09
  • If generating a PDF from a text string is your goal, you'll have to convert it to HTML and follow my suggestion above – Valor Jan 16 '22 at 21:15
  • As I had already clarified you in my first comment that "incident.number" stores sys_id, please do not confuse it with SNOW terminology of incident number. I am aware about the differences between incident number and incident record sys_id. In my case, Incident.number was a custom script variable I created. Further I only wanted to know about Attachment API for PDF files. Same script worked like charm for other formats like word, excel or text. Thanks for sharing other alternatives for PDF file. – Vanjuli Feb 09 '22 at 02:41