Send attachments to Mid Server

prabh
Mega Expert

Hi All,

How can I send attachments from Service now instance to a folder in mid server

41 REPLIES 41

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Prabh,



You will have to write mid server script include and then using Attachment API provided by servicenow download the attachments to mid server location.


This coding has to be written in mid server script include.



Steps:


1) Send attachment record sys_id to mid server


2) Send username and password to access the attachment api



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,



Thanks for your reply. But I did not get how attachment API will download the file to Mid Server.



Do I need to create a javascript probe and from that probe i need to call mid server script include and also pass the sys_id of the attachment record


Hi Prabh,



That's correct, you will have to create javascript probe which sends the required parameters and then calls the mid server script include.


Attachment API links:


Attachment API


Attachment API - GET /now/attachment



One other alternative to Attachment API is


1) Before sending parameters to mid server script include query attachment record with that sys_id and get base64encoded stream of that attachment file


2) Send this base64encoded string as parameter to mid server script include


3) Also send file name and file type i.e pdf/doc/xls/csv/xlsx etc


4) In mid server script include take this parameter value and then create a file with the filename passed in step3 and using java code decode the base64encoded string and push that into that file and you are done.


Send an Attachment from Service Now and store it on FTP server via MID server | Gyan'o'Mania



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

prabh
Mega Expert

Below are the scripts , I am using :



******BackgroudScript to call Mid Server script include********



var fileName ='SNOW_tesfile.csv';


var gr = GlideRecord('sys_attachment');


  gr.addQuery('sys_id', '02261899372f3a0041bb1a7943990e22');


  gr.query();


  if (gr.next()) {


          var sa = new GlideSysAttachment();


          var binData =   sa.getBytes(gr);


var strData = String(Packages.java.lang.String(binData));


var encData =GlideStringUtil.base64Encode(strData);


var ftpFilePath = "D:\\Test\\";


          var jspr = new JavascriptProbe('MIDserver');


          jspr.setName('FileToMidServer');


          jspr.setJavascript("var ddr = new AttachmentSender();res = ddr.execute();");


          jspr.addParameter("targetFileName",fileName);


          jspr.addParameter("encodedData",encData);


          jspr.addParameter("targetPath",ftpFilePath);


          jspr.create();


          gs.log('Completedeck Mid Server log');


}



******Mid Server Script Include******************



var AttachmentSender = Class.create();


AttachmentSender.prototype = {


    initialize : function() {


               


          this.MIDSERVER_FILE_PATH = "D:\\Test\\";          


          this.MIDSERVER_FILE_NAME = probe.getParameter('targetFileName');


         


          this.Encoded_Data = probe.getParameter('encodedData');


         


          this.useProxy = this.getConfigParameter("mid.proxy.use_proxy");


          if (this.useProxy){


                this.proxyHost =this.getConfigParameter("mid.proxy.host");


                this.proxyPort =this.getConfigParameter("mid.proxy.port");


                this.proxyUser =this.getConfigParameter("mid.proxy.username");


                this.proxyPass =this.getConfigParameter("mid.proxy.password");


          }


//     this.password = this.getConfigParameter("mid.instance.password");


          this.user = ms.getConfigParameter("mid.instance.username");


        this.password = ms.getConfigParameter("mid.instance.password")


          this.targetPath = probe.getParameter('targetPath');


          this.targetFileName = probe.getParameter('targetFileName');


         


},


   


    getConfigParameter: function(parm){


          var m= Packages.com.service_now.mid.MIDServer.get();


          var config = m.getConfig();


          var res = config.getParameter(parm);


          var res2 = config.getProperty(parm);


          if (res){


                return res;


          }


          else if (res2){


                return res2;


          }


          else{


                config = Packages.com.service_now.mid.services.Config.get();


                return config.getProperty(parm);


          }


         


    },


         


    saveToFile: function(targetPath) {


          var tmpLoc;


          var result = true;


          var strContent=new Packages.com.glide.util.Base64().decode(this.Encoded_Data);


         


          try{


                tmpLoc = this.MIDSERVER_FILE_PATH + this.MIDSERVER_FILE_NAME;


               


                var fos = new Packages.java.io.FileOutputStream(targetPath);


               


                //for(var index=0 ; index ;0);


    }


      catch(e){


     


      }},


     


    execute: function() {


          var result = '';


       


          var pushRes = true;


         


          var saveRes = this.saveToFile(this.targetPath+ this.targetFileName);


        return result;


    },


   


    type : "AttachmentSender"


};





These scripts are not working. In ecc queue, i am getting below error:



<?xml version="1.0" encoding="UTF-8"?><results error="Caused by error in JavaScript probe 'FileToMidServer' at line 1&#10;&#10;==&gt;     1: var ddr = new AttachmentSender();res = ddr.execute();&#10;" probe_time="563" result_code="900000"><result error="Caused by error in JavaScript probe 'FileToMidServer' at line 1&#10;&#10;==&gt;     1: var ddr = new AttachmentSender();res = ddr.execute();&#10;"><output>Evaluation error: Caused by error in JavaScript probe 'FileToMidServer' at line 1