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 first method (creating a Business Rule to handle the calculations). The downside to this method is that it only calculates the number of attachments when one is inserted or deleted from the record. This means that old records that already have attachments will not be calculated. However, an advantage to this method is a quick processing speed. So, if speed is 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 on. I’ll use the Incident table for this example.
Now we need to create a new field for Attachments. Make sure the default value is set to 0.
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.
Create an Attachment Counter Business Rule
The final step to adding our attachment icon is to create a Business Rule to count the number of attachments on each record. We want the Business Rule to run every time an attachment is added or removed. Using the Filter Conditions, you can have the Business Rule run for multiple tables as long as they each have the Attachments field and a Field Style to go with it.
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 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | (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()) { task.u_attachments = num; task.autoSysFields(false); //Don't set the lastUpdatedTime or the Simultaneous Update Alert will likely get triggered task.setWorkflow(false); //Don't allow other business rules to run, otherwise multiple notifications will likely be sent task.update(); } } })(current, previous); |
Once the Business Rule is complete, the attachment icon should appear on the table, showing how many attachments there are for each record.
Hi Aylee,
very nice solution, thank you for sharing.
I have implemented similar solution, only flag instead of count of attachments. I would like to share what I had to do additionally in our case. I have created before insert business rule on Task table to ensure that flag has correct value even if somebody attaches a file before the task is even inserted (in the new form view). Regarding old task records, I have iterated through all of them and updated the flag accordingly via one-time-run Fix Script. Maybe this information can help somebody.
Best regards,
Dominik
Very nice work