+ Start a Discussion

Count on Attachments

Hi Everyone, 

i have a scenario where i have to count the attachments put it ina field on the parent record.


i have custom object called Opportunity Line items. in that object we have notes and attachments as a related list. 

and we have a field called count(checkbox) on opportunity line item. 


if the user comes and attach any document this field(count) on opportunity line item need to true. 



can anyone suggest me with some sample code how to do this.


thanks in advance.






As attachments are stored in Attachment object, you can query on Attachment object, use size() method to count the attachments on particular record.


List<Attachment> at=[Select id from Attachment where parentId=:'OpportunityLineItemRecordId'];
Integer count=at.size();
System.debug('Count is '+count);


Hope this helps.:)