Attachment count

Riaz3
Giga Expert

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.

1 ACCEPTED SOLUTION

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

current.table_name == "ast_contract"
 
Which will provide the values for the queries. And as I already mentioned, the result was being fed to the "u_attachments" integer field on the Contract record. So you would set the 5th line in the "setAttachmentNum(num)" function to:
task.u_attachment_count = num;
 

View solution in original post

11 REPLIES 11

Michael Jones -
Giga Sage

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

find_real_file.png

 

If this was helpful or correct, please be kind and remember to click appropriately! Michael Jones - Proud member of the CloudPires team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

Riaz3
Giga Expert

Thanks, I will give this a try, it looks promising 

Ian Mildon
Tera Guru

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

Thanks Ian, what table should this run on? 

Also, how can we get it to add the number to a field?

Thanks