The CreatorCon Call for Content is officially open! Get started here.

[Question] Upload attachment via script

kelvin_du
Giga Contributor

Hello all,

I have to write a script (perhaps it's a scheduled job) to upload the attachment from my computer.

For example:

There are 10 records in my Incident table (without attachment).

There are 10 folders in my computer, each folder contains files which I want to attach to a specific record. 10 folders correspond to 10 records.

Run the script to upload all the attachment to the records.

I do not have any idea to do this. It should be nice if you guys give me a hand on this.

Thanks a lot

9 REPLIES 9

Hi ,


i have requirement same as like   kelvin   have.


My requirement is like this ,the folder present in ftp server will consists of XML and png. With data sources i am getting XML data and creating incident.


then   i need to attach the attachment(png) which is in folder to the particular incident record.


How to achieve this .






Thanks   in advance .


tdf
Kilo Expert

You could use the AttachmentCreator to upload the file and attach it to your incident.   There's an example Perl script included in the wiki that you could modify to meet your needs.



-tim


adiddigi
Tera Guru

Oh, yea Tim is right - If you want to invoke a script on your computer either Perl / Python using Web services.



This works because ServiceNow if hosted on the cloud will have a Public IP which your computer can connect to to push the attachments


I do have similar requirement for CMS portal, I used below script for Web services. 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>


prasadyellanur
Kilo Contributor

hi kelvin,


I am facing the same issue now with ftp server.


if u achieved successfully, please explain me in   detailed.