
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2020 09:37 AM
Hi,
Is it possible in any way to report on the number of attachments on a form?
Ideally, in a List view i would like a field to say Attachments, and then have the number of attachments of that record or even a true of false.
Thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-14-2020 05:12 AM
I just spotted one more thing that you'll need to change this to an After, which I found worked better.
I also set the Condition line on the Advanced tab to be (so set this for the correct table on each of your Business Rules):

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2020 09:56 AM
Out of the box, I don't know of any way to do so. Is this for a particular table or just in general?
You could always add a field to the table you want to report on and call it u_attachment_count with a default value of 0, then add a business rule to the sys_attachment table after / insert, check if the table is the one you want, and then take the sys_id to query the record and increment the count of u_attachment_count.
For example, for Incident:
(function executeRule(current, previous /*null when async*/) {
var incident = new GlideRecord('incident');
incident.get(current.table_sys_id);
incident.u_attachment_count++ ;
incident.setWorkflow(false);
incident.update();
})(current, previous);
If this was helpful or correct, please be kind and remember to click appropriately! Michael Jones - Proud member of the CloudPires team!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2020 12:22 PM
Thanks, I will give this a try, it looks promising
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2020 12:40 PM
This script when run on Insert and Update will count up and down when attachments are added or removed
(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; //match to integer field name
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);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-14-2020 12:14 AM
Thanks Ian, what table should this run on?
Also, how can we get it to add the number to a field?
Thanks