Include attachment using script

john0312
Kilo Contributor

Hi All,

Is there any way to include attachment(not link) using Script. Because I have a requirement of attaching the newly created attachment only. If I use the include attachment checkbox all the attachments will be attached.

By using below script and Event (Attachment.upload), I am able to send email notification with selected Attachment Link (but not as Attachment). To download this attachment one should have Servicenow Instance Access. My requirement is to sent complete Attachment which can must be downloaded without Servicenow Instances Access.

So I used following Mail script:

Email Notification:

In Message filed:                 ${mail_script:incident_script_1}

Email Notification Script:

Name: incident_script_1

attachLinks();

function attachLinks() {

    //Check for any attachments and add attachment links if they exist

    var gr = new GlideRecord('sys_attachment');

    gr.addQuery('table_sys_id',current.sys_id);

    gr.orderByDesc('sys_created_on');

    gr.query();

    if(gr.hasNext()){

          template.print('Attachments: \n');

                if (gr.next()) {

                      var attachLink = '<a href="' + gs.getProperty("glide.servlet.uri") + gs.generateURL(gr.getTableName(),gr.sys_id) +   '">' + gr.file_name + '</a>';

                      template.print(attachLink +   '\n');

                }

          template.print('<hr/>');

    }

}

Any Help in what changes do I need to make in the script. Any help would be appreciated. Thank you.

2 REPLIES 2

john0312
Kilo Contributor

Any help with this question?


jpavanaryan
Tera Expert

Hello Rohit,



I do have similar requirement for CMC portal, I used below script. This can be used for CMS and UI pages (I guess)



<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


<html>


<head>



</head>


<body>


  <h2>File Upload test   </h2>   <br/> <br/>



  <form>


  <input type="file" id="fileUpload" />  


  <input type="button" id="uploadButton" onclick="uploadFileTest()" value="Upload"/>


  <br/> <br/>



  <script>


  function uploadFileTest()


  {


    var obj=document.getElementById('fileUpload');


    var file=obj.files[0];


    var fileName=obj.files[0].name;


            var encodedFile='';


  //Encode file


  var reader = new FileReader();


  reader.onload = function(readerEvt)


  {


  var binaryString = readerEvt.target.result;


  encodedFile= btoa(binaryString);


              var xhr = new XMLHttpRequest();


  xhr.withCredentials = true;


  xhr.addEventListener("readystatechange", function ()


  {


    if (this.readyState === 4) {


  console.log(this.responseText);


    }


  });



  xhr.open("POST","https://<instancename>.service-now.com/api/now/table/ecc_queue");


  var data='{"agent":"AttachmentCreator","topic":"AttachmentCreator","name":"'+fileName+'","source":"u_itinerary_attchements:977ae444db2312009c9631b0cf9619a7","payload":"'+encodedFile+'"}';


  xhr.setRequestHeader("authorization", "Basic anBhdmFuOmpwYXZhbg==");


  xhr.setRequestHeader("Content-Type", "application/json");


  xhr.setRequestHeader("Accept", "application/json");


  xhr.setRequestHeader("cache-control", "no-cache");


  xhr.send(data);


  };


  reader.readAsBinaryString(file);




  }


  </script>



  </form>


</body>


</html>


</j:jelly>