(SM) Support Tip: JavaScript to write a txt file / add an Attachment to a ticket

JavaScript can manipulate text files on the server, this gives the capability to import data and manipulate it with custom code, for example:


  1. JavaScript to WriteFile:

         //This writes into the RUN directory of the server path 

        var filePath = ".\\mytest.txt";


        The “t” indicates this is a text file

        var FileType = "t";


       // Line Feed

       var sLF = String.fromCharCode(10);


       fileObject = vars.$lo_ucapex;


       // Add a line feed between capability words

       fileObject = fileObject.join(sLF);




       // Add the file as an attachment to Incident IM10001

       addAttachment( “IM10001”, filePath );



2.  JavaScrip to addAttachment



function addAttachment(incidentID,fileName){


var f = new SCFile('incidents');

var rc = f.doSelect("incident.id=\"" incidentID "\"");



       if( rc == RC_SUCCESS ){

       var attachObj = new Attachment();

       attachObj.type = "text";

       attachObj.name = fileName;


       var source = readFile(fileName, "t" );

       attachObj.value = source; 


       var attachmentID = f.insertAttachment( attachObj );