title: Unable to view PDF attached with ServiceNow table records
body: <p>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.</p>
  <p>Code snippet</p>
  <pre><code>(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);&lt;br&gt;
            gs.info('The PDF attachment sys_id is: ' + agr);
      }catch(err){
           gs.log('Got Error: ' + err);
           gs.info(err);
      }
  })()
  </code></pre>
  <p>I also tried &quot;AttachmentCreator&quot; with ecc_queue within script but same error occurs. Below is code for it.</p>
  <pre><code>(function execute()
      {var attCreator = new GlideRecord('ecc_queue');
      attCreator.agent = &quot;AttachmentCreator&quot;;
      attCreator.topic = &quot;AttachmentCreator&quot;;
      attCreator.name = &quot;Test.pdf&quot; + &quot;:&quot; + &quot;text/pdf&quot;; 
      //Also tried, &quot;Test.pdf:application/pdf&quot;
      attCreator.source = &quot;incident&quot;+&quot;:&quot;+ 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(&quot;Is valid base64 format in ecc_queue ? &quot;+ isValid);
      attCreator.payload = base64String; //base64 content of the file
      attCreator.insert();
  })()
  </code></pre>
  <p>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.</p>
  <p>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.</p>
  <p><a href="https://i.stack.imgur.com/X69Ja.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/X69Ja.png" alt="Error Message - Opened PDF fails to load" /></a></p>
  <p>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:</p>
  <ul>
  <li>05:38:38.411 Security restricted: File type is not allowed or does not match the content for file Test.pdf</li>
  <li>05:38:38.410 Security restricted: MIME type mismatch for file: Test.pdf. Expected type:application/pdf, Actual type: text/plain</li>
  <li>05:38:38.394 App:XYZ App x_272539_xyz_ap: Is valid base64 format in ecc_queue ? true</li>
  </ul>