ServiceNow provides users with an option to add attachments to records. However, the user cannot see if a record has an attachment unless they open it up. After playing around with different ideas, I came across two straightforward solutions:
Method 1:Â Creating a Business Rule to calculate the number of attachments whenever one is added or removed.
Method 2:Â Adding a Calculated Value to an attachments field.
In this post, I’ll go through the second method (using a Calculated Value to count attachments). The downside to this method is that the processing speed is slower since it runs every time the list loads. However, an advantage to this method is that it counts old attachments as well as new ones. So, if speed is not an important factor for you, this is the way to go.
Add Attachment Field to the Dictionary
So the first thing we need to is open up the dictionary to the table we want the attachment icon to show up on. I’ll use the Incident table for this example. Now we need to create a new field for Attachments.
For my field, I set the following values in the advanced view:
Table:Â Incident
Type:Â Integer
Column Label:Â Attachments
Column Name:Â u_attachments
Next, I set the Calculated Value. Here is the script that I used to count the number of attachments:
1 2 3 4 5 6 7 8 9 10 11 12 13 | (function calculatedFieldValue(current) { var attachCount = new GlideAggregate('sys_attachment'); attachCount.addQuery('table_sys_id', current.sys_id); attachCount.addQuery('table_name',current.getTableName()); attachCount.addAggregate('COUNT'); attachCount.query(); var numAttachments = 0; if (attachCount.next()) { numAttachments = attachCount.getAggregate("COUNT"); } return numAttachments; })(current); |
Once the Attachments field is created, configure the List Layout of the table to add our field.
Create a Field Style
Now that we have the Attachments field created and in place, we will need to create a style for it. Navigate to System UI > Field Styles. The form should be filled out as follows:
The Value above is making sure that the style only applies when there is at least one attachment on the record. Getting the background image was a little tricky. I came across a blog on ServiceNow Icons and Images that said you could find the icons that ServiceNow uses by typing image_picker.do in the Navigator. Using this method, I picked a paperclip icon for the Attachments image.
Result
Now that we have a field style for an attachments field that calculates the number of attachments, our table should look like this:
nice one !