function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Divya_RemedyforceDivya_Remedyforce 

How to get the number of attachments added to a custom object

Hi ,

I want to write a trigger on Attachment object to retrieve the no. of attachments added to a custom object .

can anyone help me with a solution

souvik9086souvik9086

Yes, you can write a trigger on Attachment through Force.com IDE not in orgs.

You can query there for which custom object you want to fetch the attachments.

select id ,(select id,name from Attachments) from Customobject__c

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Divya_RemedyforceDivya_Remedyforce

Souvik ,I have tried to write a trigger on Developer Console.

I am not able to get how can I access the no. of attachments in the custom object.

Is there any function that can be used 

souvik9086souvik9086

If you want to get no of attachments of a particular object why are you creating trigger?

 

Create an apexclass and within that query like this

 

 List<Customobject__c> cusObjList = [select id ,(select id,name from Attachments) from Customobject__c];

Then in VF page display this list in table

 

<apex:pageblocktable value="{!cusObjList.Attachments}" var="attachment">

<apex:column>

<apex:outputfield value="{!attachment.name}"/>

</apex:column>

</apex:pageblocktable>

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

Divya_RemedyforceDivya_Remedyforce

Actually in the custom object(Change Request) there is lokkup field (Status) and a check box(PIR) .If the the status is changed from Completed to Closed IP is mandatory to check.Now once this checkbox is checked it is mandatory to add an attachment (there should be an error msg displayed) .I have written a trigger on custom object that displays the error msg if there is no attachment but if there is an attachment added already ( before completed status) the trigger doesnt work. Thats why I tried  writing trigger on Attachment .

This was the trigger on custom object:

 

trigger PIRattachment on BMCServiceDesk__Change_Request__c (before insert, before update)
{

Map<Id,BMCServiceDesk__Change_Request__c> changeAttachments = new Map<Id,BMCServiceDesk__Change_Request__c>(
[select Id,(select Id from Attachments) from BMCServiceDesk__Change_Request__c WHERE Id IN :Trigger.new]);

for(BMCServiceDesk__Change_Request__c chg:Trigger.new)
{
if(chg.PIR__c == true &&
(!changeAttachments.containsKey(chg.id) ||
changeAttachments.get(chg.id).Attachments == null ||
changeAttachments.get(chg.id).Attachments.size()==0))
{

chg.PIR__c.addError('Attach a file to this change request before closing it.');
}
}
}

 

Avidev9Avidev9
So lets summarise info that you provided.
1. User needs to attach a attachment once the checkbox PIR is checked ?
2. What if the record already contains attachment ? Should this show an error ?
Divya_RemedyforceDivya_Remedyforce

1. To change status from 'Completed' to 'Closed' , PIR should be checked.

 

2. When PIR is checked it is mandatory to add an attachment at that time only.(this attachment is PIR specific)

 

3. If an attachment already exists we need to check if it was added after the status was change to 'Completed' .If the attchment was added prior to it , there should be a msg notifying the user to add attachment