Get the name of attachment when deleted

F_bio Santos
Kilo Sage

Hi everyone, Im trying to get the name of the attachment when I delete it from a record, Im using the "Ticket Attachments" widget where I changed the code a bit so it shows the file name. Im having a problem with this, when I have more than 1 attachment and delete one, it always show's the wrong one on the activity stream.

Example:

Attachment1
Attachment2

If I delete attachment1 it says that I deleted the attachment2, and if I delete the attachment2 it says that I deleted the attachment1 ...

F_bioSantos_0-1698139675841.png


Code:

 

Server Script: 

(function() {
    data.sys_id = input.sys_id || options.record_id || $sp.getParameter("sys_id");
    data.table = input.table || options.record_table || $sp.getParameter("table");
    data.appendToId = options.appendToId;
    data.table = _getActualTable(data.table, data.sys_id);
    data.maxAttachmentSize = parseInt(gs.getProperty("com.glide.attachment.max_size", 1024));
    if (isNaN(data.maxAttachmentSize))
        data.maxAttachmentSize = 24;
    data.largeAttachmentMsg = gs.getMessage("Attached files must be smaller than {0} - please try again", "" + data.maxAttachmentSize + "MB");
    data.attachmentSuccessMsg = gs.getMessage("Attachment successfully uploaded");
    data.noDragDropMsg = gs.getMessage("Attachment drap-and-drop not enabled - use attachment button");
    data.noAttachmentsMsg = gs.getMessage("There are no attachments");

    var attachment = new GlideRecord('sys_attachment');

    if (!data.table || !data.sys_id)
        return;
	
    var gr = new GlideRecord(data.table);
    if (!gr.isValid())
        return;

    if (!gr.get(data.sys_id))
        return;

    if (input && input.action == "deleted") {

        var att = new GlideRecord('sys_attachment');
        att.addQuery('table_sys_id',data.sys_id);
        att.query();
        if (att.next()) {
           //gs.addInfoMessage("Attachment " + att.file_name + " is deleted");
					 gr.comments = input.action + " the attachment: " + att.file_name;
        }
        gr.update();
    }

 console.log(data.state);
	
    data.canWrite = gr.canWrite();
    data.canAttach = gs.hasRole(gs.getProperty("glide.attachment.role")) && GlideTableDescriptor.get(data.table).getED().getAttribute("no_attachment") != "true";
    data.canRead = gr.canRead();
    data.canDragAndDrop = gs.getProperty('glide.ui.attachment_drag_and_drop', 'true') === 'true';

    function _getActualTable(table, id) {
        if (!table)
            return table;

        var rec = new GlideRecord(table);
        if (!rec.isValid())
            return table;

        if (rec.get(id) && rec.getValue('sys_class_name')) {
            return rec.getValue('sys_class_name');
        }
        return table;
    }
	
   	data.state = gr.getValue('state');
	
})();

 

In the code above, this is where it returns the deleted messages:

    if (input && input.action == "deleted") {

        var att = new GlideRecord('sys_attachment');
        att.addQuery('table_sys_id',data.sys_id);
        att.query();
        if (att.next()) {
           //gs.addInfoMessage("Attachment " + att.file_name + " is deleted");
					 gr.comments = input.action + " the attachment: " + att.file_name;
        }
        gr.update();
    }



0 REPLIES 0