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

Hi Ankur,

 

Can you help me to export records from ServiceNow to shared folder

I tried to accomplish it via Export set and It got successfully completed but didn't find the CSV file on folder

I believe it's something related to MID Server because the export logs tells me about

1- Export started

2- Created an attachment 'xxx_users.csv' on MID Server Attachment table

3- Transferred xxx_users.cs to the MID Server 'xxxxxx-1'

Finally I don't see any file on location

 

Thanks

 

Hi   Prabh,


Could you please share the full code


Did you find any solution?


Hi



This is borrowed code from other posters and figuring out where all the issues are so I cant claim as my own. This worked for me as is after fixing up. You just need to update file paths etc. Good luck



/***************************************************************************


This example shows how to copy an attachment from the attachments table to a file location on  


a mid server.


**************************************************************************/


/***************************************************************************


Use a version of this script to initiate the process of copying a file e.g. UI action or business rule. Adapt accordingly.


This script creates a probe recrod on the ecc output queue. This is processed by the mid server which writes the response back to input.


You can troubleshoot for errors by looking on the ecc_queue.


**************************************************************************/


var fileName ='baan-2017-08-10.txt';         // the name of the file in the destination folder on mid server


var gr = GlideRecord('sys_attachment');


  gr.addQuery('table_sys_id', 'd5396a8c0f640300627f8b9ae1050e35'); //sys_attachment we copied. This needs some more query parameters to be more specific.


  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 = "C:\\BaanExports\\";   // file path on mid server where file will be copied to.


          var jspr = new JavascriptProbe('mid1');   // Creates a dynamic probe for the mid server specified in quotes.


          jspr.setName('FileToMidServer');   //This is a arbitrary name for the dynamic probe


          jspr.setJavascript("var ddr = new AttachmentSender(); res= ddr.execute();"); //this is the script the probe must run. In this case it calls a mid server script include.


          jspr.addParameter("targetFileName",fileName);


          jspr.addParameter("encodedData",encData);


          jspr.addParameter("targetPath",ftpFilePath);


          jspr.create();


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


}


/***************************************************************************


This script is a Mid server script include. (Look under mid server section in your instance)


Create a new script include and set name for the class. e.g. below is name = AttachmentSender


****************************************************************************


var AttachmentSender = Class.create();


AttachmentSender.prototype = {


    initialize : function() {


             


          this.MIDSERVER_FILE_PATH = "C:\\BaanExports\\";         //Not exactly sure why this is here.


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


          ms.log("Running Initialise Section");           //Use this syntax to log messages to mid server log file for troubleshooting


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



/***************************************************************************


This sections queries the mid server config file to get mid properties in order to connect to instance


**************************************************************************/


            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);


          }


       


    },


   


/***************************************************************************


End of get properties section


**************************************************************************/


/***************************************************************************


The magic happens here


**************************************************************************/


// Called from execute function below to save file


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(tmpLoc);


              for (var index = 0; index < strContent.length; index++) {


                      fos.write(strContent[index].toString());


              }


              fos.close();


              this.log('File saved to: ' + tmpLoc);


      } catch (e) {


              result = false;


      }},



//This function below is called from the dynamic probe. It then calls the function above to write out the actual file


      execute: function() {


//ms.log("I am running the execute section");


          var result = '';


                  var pushRes = true;


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


        return result;


    },


        type : "AttachmentSender"


};


Thanks for providing the script. It didn't work for me, I can't see any error as well in ECC queue to troubleshoot the issue.