Add the uploaded file to sys_attachment table

NamanChaturvedi
Tera Contributor

Hi
I am trying to create a record on the sys_attachment table through the file that I am uploading via a UI action.

But that file doesn't seem to show up on the sys_attachment table.

On the UI action I have called the UI page from where I am putting the GlideAjax call passing the file to Script Include
On Script Include I am trying to create a new record on the sys_attachment table by passing the column values line by line.

Need help in getting this resolved .!!!!!

Script Include Code:

var DataSysAttachAndParseToImportSet = Class.create();
DataSysAttachAndParseToImportSet.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    addToSysAttachment: function() {
        var myUploadedFile = this.getParameter('sysparm_file');
		// var fileContent = this.getParameter('sysparm_fileContent');
        gs.addInfoMessage("File uploaded is : " + myUploadedFile);

        // var attachment = new GlideSysAttachment().write(myUploadedFile, 'sys_attachment', 'Uploaded Excel File', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

        var attachment = new GlideRecord('sys_attachment');

        attachment.initialize();
        attachment.file_name = myUploadedFile;
        attachment.content_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; // MIME type for Excel files
        attachment.table_name = 'incident'; // Example: Attach to an incident record
        attachment.sys_created_by = gs.getUserID();
        // attachment.sys_created_on = new GlideDateTime();
        // attachment.file_stream = fileContent; // Set the file content

        // Insert the attachment record
        attachment.insert();

        gs.info('File uploaded successfully as attachment: ' + attachment.getDisplayValue());

        // var parser = new GlideExcelParser();
        // var excelData = parser.parseExcel(attachment);

        // gs.addInfoMessage("Excel data is :" + excelData);
        return attachment.getDisplayValue();
    },
    type: 'DataSysAttachAndParseToImportSet'
});


 

 

UI Page script and Ajax Call : 

<input type="file" id="fileInput" />
    <button onclick="uploadFile()">Upload</button>

    <script>
        function uploadFile() {
            var fileInput = document.getElementById('fileInput');
            var file = fileInput.files[0];

            // You can add further logic to handle file upload here
            alert('File uploaded: ' + file.name);

			var ga = new GlideAjax('DataSysAttachAndParseToImportSet');
			ga.addParam('sysparm_name' , "addToSysAttachment");
			ga.addParam('sysparm_file' , file);

			ga.getXML(myFunc);

			function myFunc(response){
				
			}
        }
    </script>

0 REPLIES 0