Total Number of attachments

manisha3014
Kilo Contributor

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.

8 REPLIES 8

Hi Vinoth,



Thank you for your reply. Can you please share script even please.


This doesn't help when an attachment is deleted from the incident record!


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);


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();