+ Start a Discussion

Want to find out the count of number of attachments


Hi All,


My requirement is on a custom page to set a "attachment present" field to Yes if there is any attachment exists or to No  if no attachment exists .


in a way find the count of the attachments and set this filed value. Can i write a trigger on attachment object using force.IDE or is there any alternate way? Please suggest. The attachments will be uploaded using standard notes and attachment related list.


I can find some threads on the same but i want to cross check whether there is way to do this


Thanks in advance



I've done this in the past by using a trigger on the attachment object.  When an attachment is created, I increment a counter field on the parent object, and when an attachment is deleted I decrement the count.  


If you are in a custom page that is backed by an object instance, you should be able to do this via SOQL.  


Integer numAtts=[select count() from attachment where parentid=:recId];

 where recId is the id of the record being managed by the page.


Thanks a lot