Total Number of attachments
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-31-2017 04:41 AM
Hi All,
In incident form, i have created a field "total attachment" in that field i want to get total number count of attachment in the form. How to get that one. Anyone please help me.
Thank you in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-31-2017 05:49 AM
Hi Vinoth,
Thank you for your reply. Can you please share script even please.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-31-2017 06:21 AM
This doesn't help when an attachment is deleted from the incident record!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-31-2017 06:32 AM
Just add the follwing code to the "Calculation" script field of my approach mentioned above and you're fine:
(function calculatedFieldValue(current) {
// Add your code here
var count = 0;
var att = new GlideAggregate('sys_attachment');
att.addQuery('table', current.sys_class_name);
att.addQuery('table_sys_id', current.sys_id);
att.addAggregate('COUNT');
att.query();
if (att.next()) {
count = att.getAggregate('COUNT');
}
return count; // return the calculated value
})(current);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-31-2017 06:35 AM
Hi Manisha,
try something as similar and it will work
Name: Update u_attachment_count
Table: Attachment [sys_attachment]
When: after [insert]
Condition: current.table_name == 'incident'
Script:
//since we are on the sys_attachment table, we get the KB record into "x"
var x = new GlideRecord('incident');
x.addQuery('sys_id', current.table_sys_id);
x.query();
x.next();
while(x.next())
//now, we do a second glide record for the attachment table to count all the attachments
var y = new GlideRecord('sys_attachment');
y.addQuery('table_sys_id',x.sys_id);
y.query();
//which we then put back into the "x" record and update.
x.u_attachment_count = y.getRowCount();
x.update();