How can we check if the record has an attachment in list view

divya_dattatray
Kilo Contributor

How can we check if the record has an attachment in list view. As now we have to open every record to see if the record has an attachment.

so want to know if record has an attachment from list view itself, how we can achieve this.

1 ACCEPTED SOLUTION

ChrisBurks
Mega Sage

Another way that is possible is using the hasAttachments() on the sys_ui_style table for the record. For instance if I wanted an icon or color change to indicate that the record has an attachment in the list view I could setup a style like so:


sys-ui-style.png



And if the record has an attachment it will display like below (I didn't have a paperclip icon 😞



attachment-list-view.png


(Notice the little yellow document icon)



I know, aesthetically it's not all that pretty. But it's an option. Perhaps maybe if the field were just an empty field and then this style placed on that field it may look better.


View solution in original post

30 REPLIES 30

The approach I took was to create a business rule on the attachment table. The reason I did this is because adding an attachment actually doesn't update the associated record. Thus the business rule wouldn't have executed on the direct table.

The attachment record has both the sys_id (table_sys_id field) and the table name (table_name field) of the associated record thus everything that is needed is there.

Here are the steps:

1) Create an image field on the desired table desired to display the attachment indicator

2) Find an existing image to be used from the image library (db_image) or upload a new one to the images library. Copy the sys_id of it.

find_real_file.png

3) Navigate to the attachments table (sys_attachment) list view and filter with the image sys_id copied from the previous step. You'll notice that images added to an image field on a table are actually attachments with a special table name used
find_real_file.png

4) Copy the sys_id of the image attachment

find_real_file.png

5) Right click on a header of the attachment list view and choose to configure business rules

6) Create a new business rule that executes on insert, update, or delete (remember an attachment could get removed). Also check the "Advanced" check box and add the following script:

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

    var currTableSysId = current.table_sys_id;
    var recGr = new GlideRecord(current.table_name);
    recGr.get(currTableSysId);
    var op = current.operation().toString().toLowerCase();
    if(recGr.isValidRecord()){
        if(op.match(/insert|update/)){
            recGr.setValue('u_attachment_flag', '4f887026ffe031003f07ffffffffff21');
        }

        if(op.match(/delete/) && !hasMore()){
            recGr.u_attachment_flag = '';
        }

        recGr.update();
    }

    function hasMore(){
        var sa = new GlideRecord('sys_attachment');
        sa.addQuery('table_sys_id', currTableSysId);
        sa.query();
        var count = sa.getRowCount();

        return count > 0;
    }

})(current, previous);




In this code the "u_attachment_flag" is the new field that was created from step 1. It is being assigned the sys_id gathered from step 4. In this case I used the image that has been used throughout this post so the sys_id should be the same in any instance as it's an OOB image.

 

Disclaimer: I didn't thoroughly test this code to catch all ins and outs of it. I'll leave that up to you. Hopefully this helps.

Thanks for this, while waiting for the code, i was able to figure it out, see my replies below for my step by step instructions for 2 different fields i wound up creating for this need.  ill most likely wind up pushing both out allowing my users to decide if they want a true/false type of field, or a field with an actual count.

Jeffrey Siegel
Mega Sage

SO i was able to complete this using this link as a reference:

 

Adding an Attachment Icon for Lists of Records – Method 1 | John Andersen (john-james-andersen.com)

 

the images aren't there in the post, (so i will describe below), however the video on the bottom exists and walks through it, except the code needed isn't visible in the video, but luckily its the only image that still did stay in the original post.

 

basically,

 

1) Create a field in my incident table - "u_attachment_count" - "Attachment Count" - type: Integer - No Default value, as i perfer it to be empty instead of 0

2) Create a style for this in System UI - Field Styles -

     a) Table - Incident

     b) Field Name - Attachment Count

     c) value - javascript:current.u_attachment_count > 0

     d) style - background-image: url('/images/paperclip.gif');
                   background-repeat: no-repeat;
                   background-position: center center;
                   text-align: center;

3) Create business rule -

Name: Attachment Counter
Table: Attachment [sys_attachment]

Active: true
Advanced: True

When to run:
When: after
Order: 100

Insert: True
Update: True
Delete: True
Query: True

Advanced:
Script (I didn't like the 0 count in the OP's code, so instead I set it to display nothing if no attachment, see my adjusted code below):

(function executeRule(current, previous /*null when async*/) {
checkAttachment();
	
	function checkAttachment(){
		var attachCount = new GlideAggregate('sys_attachment');
		attachCount.addQuery('table_sys_id', current.table_sys_id);
		attachCount.addQUery('table_name', current.table_name);
		attachCount.addAggregate('COUNT');
		attachCount.query();
		
		var numAttachments = 0;
		if (attachCount.next()){
			numAttachments = attachCount.getAggregate("COUNT");
			setAttachmentNum(numAttachments);
		}
		else{
			setAttachmentNum(numAttachments);
		}
	}
	
	function setAttachmentNum(num){
		var task = new GlideRecord(current.table_name);
		task.get(current.table_sys_id);
		if(task.isValidRecord()){
                        //my adjusted code is in this if statement.  original code just set task.u_attachment_count = num; without the if/else verification
			if(num > 0)	{
				task.u_attachment_count = num; //sets the field to the number of attachments
			}
			else {
				
				task.u_attachment_count = ""; //sets the field to empty if no attachments
			}
			task.autoSysFields(false); //Don't set the last UpdateTIme or the Simultaneous Update
			task.setWorkflow(false); //Don't allow other business rules to run otherwise multiple notices will appear
			task.update();
		}
	}
	

})(current, previous);

Now i also created a true false field, depending on the itil users needs, they can use that, (may be easier to filter match or filter out with true false.

1) create field in incident table: Attachment - u_attachment - type Boolean true/false

2) add field style:

     a) Table: incident

     b) field name: Attachments

     c) value: javascript:current.hasAttachments()

     d) style:background-image: url('/images/paperclip.gif');
                 background-repeat: no-repeat;
                 padding-left: 4px;
                 background-size: 14px;

the only way to set/change the true false is to add an attachment and then make a change in the actual record, using the business rule i found for this script, however leveraging the above business rule, which works off the attachment table instead of incident,  the below adjustments will adjust this true/false without having to edit the actual incident more than adding the attachment:

from:

if(num > 0)	{
				task.u_attachment_count = num; //sets the field to the number of attachments
			}
			else {
				
				task.u_attachment_count = ""; //sets the field to empty if no attachments
			}

to:

			if(num > 0)	{
				task.u_attachments = "true";
				task.u_attachment_count = num;
			}
			else {
				task.u_attachments = "false";
				task.u_attachment_count = "";
			}

 

3) create a new business rule (if using the above business rule with the modification suggested, i dont think the below is really necessary, however its still active in my system so ill include it for you):

Name: Attachment
Table: Incident

Active: True
Advanced: True

When to run:
When: Display
Order: 10

Delete: True
Query: True

 

Advanced:
Script:

(function executeRule(current, previous /*null when async*/) {
if(current.hasAttachments()){		
	current.setValue("u_attachment","true");
	
	}
	else if(!current.hasAttachments())
		{
			current.setValue("u_attachment","false");
			
			
		}

})(current, previous);

The only issue i have with either of these, that it doesnt account for attachments already in the system.  i had to manually set the true/false or the count for all the tickets that already exist in my dev instance.  anyone know how to have existing tickets automatically updated to account for this new script?