Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to publish in work notes when an attachment is deleted?

marioalberto
Kilo Contributor

Hi all,

I have as a requirement to update the work notes every time a file is attached or deleted, I have researched and there are events such as attachment.read and attachment.deleted that can help me to solve it.

If someone can help me to call them from a business rule I would appreciate it very much.

Greetings to all.

1 ACCEPTED SOLUTION

siva_
Giga Guru

Every Time a file is deleted from the record , you will be losing the access to the file name by the time the event attachment.delete is called as the file donot exist in the database 

You can use a BR for this requirement on the attachment table (sys_attachment) and you can log that in the ticket as per your requirement 

Create a Business Rule on the "sys_attachment" table. This will log a work note in the record that it was removed.
-Name: "Delete Attachment - Update Record"
-When: Before
-Delete: true



updateRecordForAttachment();// calling this function before deleting
function updateRecordForAttachment(){
var gr = new GlideRecord(current.table_name);//if you want only for a particular table you can even add a condition in BR to run only for that table
if(gr.get(current.table_sys_id)){
gr.work_notes = "Attachment: " + current.file_name + " has been removed.";
gr.update();
}
}
Hope this helps

Mark this response as correct if that really helps

Thanks,
Siva

View solution in original post

3 REPLIES 3

siva_
Giga Guru

Every Time a file is deleted from the record , you will be losing the access to the file name by the time the event attachment.delete is called as the file donot exist in the database 

You can use a BR for this requirement on the attachment table (sys_attachment) and you can log that in the ticket as per your requirement 

Create a Business Rule on the "sys_attachment" table. This will log a work note in the record that it was removed.
-Name: "Delete Attachment - Update Record"
-When: Before
-Delete: true



updateRecordForAttachment();// calling this function before deleting
function updateRecordForAttachment(){
var gr = new GlideRecord(current.table_name);//if you want only for a particular table you can even add a condition in BR to run only for that table
if(gr.get(current.table_sys_id)){
gr.work_notes = "Attachment: " + current.file_name + " has been removed.";
gr.update();
}
}
Hope this helps

Mark this response as correct if that really helps

Thanks,
Siva

Hello Siva Kalyan Chatakonda,

 

Your help has helped me a lot to solve my request.

I appreciate your time and dedication to the colleagues we just started.

 

Greetings.

abc_12
Tera Contributor

Hi   

siva_


(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    // gs.sleep(20000);

    var recordId = current.table_sys_id;
    var tableName = current.table_name;
    gs.info("parent case" + recordId);
    var source = new GlideSysAttachment();
    var source_content = source.getContentStream(current.sys_id);

    var targetGlideRecord = new GlideRecord("incident");
    targetGlideRecord.addQuery('parent', recordId);
    gs.info("MN1");
    targetGlideRecord.query();

    while (targetGlideRecord.next()) {
        gs.info("MN12");
        var fileName = current.file_name;
        var contentType = current.content_type;
        var sourceAttachmentSysId = current.getValue('sys_id');
        var targetId = targetGlideRecord.sys_id;
        var targetTbl = targetGlideRecord.getTableName();
        // find an attachment
        gs.info("MN123");
        var gr = new GlideRecord('sys_attachment');
        gr.addQuery('file_name', fileName);
        gr.addQuery('table_name', targetTbl);
        gr.addQuery('table_sys_id', targetId);
        gr.query();
        gs.info("MN1234");
        if (gr.next()) {
            gs.info("MN12345");
            var deletedAttachmentName = gr.file_name.toString();
            gr.deleteRecord(); // Delete the attachment
            gs.info("deletedAttachmentName is" + deletedAttachmentName);

             gr.work_notes = "Attachment: " + deletedAttachmentName + " has been removed.";
             gr.update();
        }
        gs.info("MN123456");
        var attachment = new GlideSysAttachment();
        attachment.writeContentStream(targetGlideRecord, fileName, contentType, source_content);
    }

})(current, previous);

Using this code I am deleting the attachment on Case record but I want to put message in Additional comments that "filename + "delete an attachment " in Additional Comments after delete the attachment 

Can you please help on this.